Home | History | Annotate | Line # | Download | only in ossaudio
ossaudio.c revision 1.65
      1  1.65  christos /*	$NetBSD: ossaudio.c,v 1.65 2009/08/22 23:31:16 christos Exp $	*/
      2  1.22  augustss 
      3  1.27  augustss /*-
      4  1.62        ad  * Copyright (c) 1997, 2008 The NetBSD Foundation, Inc.
      5  1.22  augustss  * All rights reserved.
      6  1.22  augustss  *
      7  1.22  augustss  * Redistribution and use in source and binary forms, with or without
      8  1.22  augustss  * modification, are permitted provided that the following conditions
      9  1.22  augustss  * are met:
     10  1.22  augustss  * 1. Redistributions of source code must retain the above copyright
     11  1.22  augustss  *    notice, this list of conditions and the following disclaimer.
     12  1.22  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.22  augustss  *    notice, this list of conditions and the following disclaimer in the
     14  1.22  augustss  *    documentation and/or other materials provided with the distribution.
     15  1.22  augustss  *
     16  1.22  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.22  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.22  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.22  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.22  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.22  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.22  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.22  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.22  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.22  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.22  augustss  * POSSIBILITY OF SUCH DAMAGE.
     27  1.22  augustss  */
     28  1.38     lukem 
     29  1.38     lukem #include <sys/cdefs.h>
     30  1.65  christos __KERNEL_RCSID(0, "$NetBSD: ossaudio.c,v 1.65 2009/08/22 23:31:16 christos Exp $");
     31  1.22  augustss 
     32   1.1   mycroft #include <sys/param.h>
     33   1.1   mycroft #include <sys/proc.h>
     34   1.1   mycroft #include <sys/systm.h>
     35   1.1   mycroft #include <sys/file.h>
     36   1.6  augustss #include <sys/vnode.h>
     37   1.1   mycroft #include <sys/filedesc.h>
     38   1.1   mycroft #include <sys/ioctl.h>
     39   1.1   mycroft #include <sys/mount.h>
     40  1.26  augustss #include <sys/kernel.h>
     41   1.1   mycroft #include <sys/audioio.h>
     42  1.26  augustss #include <sys/midiio.h>
     43  1.62        ad #include <sys/kauth.h>
     44   1.1   mycroft #include <sys/syscallargs.h>
     45  1.64        ad #include <sys/module.h>
     46   1.1   mycroft 
     47   1.6  augustss #include <compat/ossaudio/ossaudio.h>
     48   1.6  augustss #include <compat/ossaudio/ossaudiovar.h>
     49   1.6  augustss 
     50  1.64        ad MODULE(MODULE_CLASS_MISC, compat_ossaudio, NULL);
     51  1.64        ad 
     52  1.16  augustss #ifdef AUDIO_DEBUG
     53  1.16  augustss #define DPRINTF(x) if (ossdebug) printf x
     54  1.16  augustss int ossdebug = 0;
     55  1.16  augustss #else
     56  1.16  augustss #define DPRINTF(x)
     57  1.16  augustss #endif
     58  1.16  augustss 
     59  1.32      tron #define TO_OSSVOL(x)	(((x) * 100 + 127) / 255)
     60  1.32      tron #define FROM_OSSVOL(x)	((((x) > 100 ? 100 : (x)) * 255 + 50) / 100)
     61  1.17  augustss 
     62  1.62        ad static struct audiodevinfo *getdevinfo(file_t *);
     63  1.31  augustss static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq);
     64  1.31  augustss static int enum_to_ord(struct audiodevinfo *di, int enm);
     65  1.31  augustss static int enum_to_mask(struct audiodevinfo *di, int enm);
     66   1.1   mycroft 
     67  1.62        ad static void setblocksize(file_t *, struct audio_info *);
     68  1.14  augustss 
     69  1.65  christos #ifdef AUDIO_DEBUG
     70  1.65  christos static const char *
     71  1.65  christos compat_ossaudio_getcmd(u_long cmd)
     72  1.65  christos {
     73  1.65  christos 	static char buf[64];
     74  1.65  christos 	switch (cmd) {
     75  1.65  christos #define _DO(_a) \
     76  1.65  christos 	case _a: \
     77  1.65  christos 		return # _a;
     78  1.65  christos _DO(OSS_SNDCTL_DSP_RESET)
     79  1.65  christos _DO(OSS_SNDCTL_DSP_SYNC)
     80  1.65  christos _DO(OSS_SNDCTL_DSP_SPEED)
     81  1.65  christos _DO(OSS_SOUND_PCM_READ_RATE)
     82  1.65  christos _DO(OSS_SNDCTL_DSP_STEREO)
     83  1.65  christos _DO(OSS_SNDCTL_DSP_GETBLKSIZE)
     84  1.65  christos _DO(OSS_SNDCTL_DSP_SETFMT)
     85  1.65  christos _DO(OSS_SOUND_PCM_READ_BITS)
     86  1.65  christos _DO(OSS_SNDCTL_DSP_CHANNELS)
     87  1.65  christos _DO(OSS_SOUND_PCM_READ_CHANNELS)
     88  1.65  christos _DO(OSS_SOUND_PCM_WRITE_FILTER)
     89  1.65  christos _DO(OSS_SOUND_PCM_READ_FILTER)
     90  1.65  christos _DO(OSS_SNDCTL_DSP_POST)
     91  1.65  christos _DO(OSS_SNDCTL_DSP_SUBDIVIDE)
     92  1.65  christos _DO(OSS_SNDCTL_DSP_SETFRAGMENT)
     93  1.65  christos _DO(OSS_SNDCTL_DSP_GETFMTS)
     94  1.65  christos _DO(OSS_SNDCTL_DSP_GETOSPACE)
     95  1.65  christos _DO(OSS_SNDCTL_DSP_GETISPACE)
     96  1.65  christos _DO(OSS_SNDCTL_DSP_NONBLOCK)
     97  1.65  christos _DO(OSS_SNDCTL_DSP_GETCAPS)
     98  1.65  christos _DO(OSS_SNDCTL_DSP_GETTRIGGER)
     99  1.65  christos _DO(OSS_SNDCTL_DSP_SETTRIGGER)
    100  1.65  christos _DO(OSS_SNDCTL_DSP_GETIPTR)
    101  1.65  christos _DO(OSS_SNDCTL_DSP_GETOPTR)
    102  1.65  christos _DO(OSS_SNDCTL_DSP_MAPINBUF)
    103  1.65  christos _DO(OSS_SNDCTL_DSP_MAPOUTBUF)
    104  1.65  christos _DO(OSS_SNDCTL_DSP_SETSYNCRO)
    105  1.65  christos _DO(OSS_SNDCTL_DSP_SETDUPLEX)
    106  1.65  christos _DO(OSS_SNDCTL_DSP_GETODELAY)
    107  1.65  christos _DO(OSS_SNDCTL_DSP_PROFILE)
    108  1.65  christos _DO(OSS_SOUND_MIXER_INFO)
    109  1.65  christos _DO(OSS_SOUND_OLD_MIXER_INFO)
    110  1.65  christos _DO(OSS_GET_VERSION)
    111  1.65  christos _DO(OSS_SEQ_RESET)
    112  1.65  christos _DO(OSS_SEQ_SYNC)
    113  1.65  christos _DO(OSS_SYNTH_INFO)
    114  1.65  christos _DO(OSS_SEQ_CTRLRATE)
    115  1.65  christos _DO(OSS_SEQ_GETOUTCOUNT)
    116  1.65  christos _DO(OSS_SEQ_GETINCOUNT)
    117  1.65  christos _DO(OSS_SEQ_PERCMODE)
    118  1.65  christos _DO(OSS_SEQ_TESTMIDI)
    119  1.65  christos _DO(OSS_SEQ_RESETSAMPLES)
    120  1.65  christos _DO(OSS_SEQ_NRSYNTHS)
    121  1.65  christos _DO(OSS_SEQ_NRMIDIS)
    122  1.65  christos #ifdef notyet
    123  1.65  christos _DO(OSS_MIDI_INFO)
    124  1.65  christos #endif
    125  1.65  christos _DO(OSS_SEQ_THRESHOLD)
    126  1.65  christos _DO(OSS_MEMAVL)
    127  1.65  christos _DO(OSS_FM_4OP_ENABLE)
    128  1.65  christos _DO(OSS_SEQ_PANIC)
    129  1.65  christos _DO(OSS_SEQ_OUTOFBAND)
    130  1.65  christos _DO(OSS_SEQ_GETTIME)
    131  1.65  christos _DO(OSS_ID)
    132  1.65  christos _DO(OSS_CONTROL)
    133  1.65  christos _DO(OSS_REMOVESAMPLE)
    134  1.65  christos _DO(OSS_TMR_TIMEBASE)
    135  1.65  christos _DO(OSS_TMR_START)
    136  1.65  christos _DO(OSS_TMR_STOP)
    137  1.65  christos _DO(OSS_TMR_CONTINUE)
    138  1.65  christos _DO(OSS_TMR_TEMPO)
    139  1.65  christos _DO(OSS_TMR_SOURCE)
    140  1.65  christos _DO(OSS_TMR_METRONOME)
    141  1.65  christos _DO(OSS_TMR_SELECT)
    142  1.65  christos #undef _DO
    143  1.65  christos 	default:
    144  1.65  christos 		(void)snprintf(buf, sizeof(buf), "*0x%lx*", cmd);
    145  1.65  christos 		return buf;
    146  1.65  christos 	}
    147  1.65  christos }
    148  1.65  christos #endif
    149  1.65  christos 
    150  1.65  christos 
    151  1.64        ad static int
    152  1.64        ad compat_ossaudio_modcmd(modcmd_t cmd, void *arg)
    153  1.64        ad {
    154  1.64        ad 
    155  1.64        ad 	switch (cmd) {
    156  1.64        ad 	case MODULE_CMD_INIT:
    157  1.64        ad 	case MODULE_CMD_FINI:
    158  1.64        ad 		return 0;
    159  1.64        ad 	default:
    160  1.64        ad 		return ENOTTY;
    161  1.64        ad 	}
    162  1.64        ad }
    163  1.16  augustss 
    164   1.1   mycroft int
    165  1.61       dsl oss_ioctl_audio(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
    166  1.61       dsl {
    167  1.61       dsl 	/* {
    168   1.1   mycroft 		syscallarg(int) fd;
    169   1.1   mycroft 		syscallarg(u_long) com;
    170  1.55  christos 		syscallarg(void *) data;
    171  1.61       dsl 	} */
    172  1.62        ad 	file_t *fp;
    173   1.1   mycroft 	u_long com;
    174   1.1   mycroft 	struct audio_info tmpinfo;
    175  1.13  augustss 	struct audio_offset tmpoffs;
    176  1.13  augustss 	struct oss_audio_buf_info bufinfo;
    177  1.13  augustss 	struct oss_count_info cntinfo;
    178  1.16  augustss 	struct audio_encoding tmpenc;
    179  1.21  augustss 	u_int u;
    180  1.13  augustss 	int idat, idata;
    181  1.28   thorpej 	int error = 0;
    182  1.62        ad 	int (*ioctlf)(file_t *, u_long, void *);
    183   1.1   mycroft 
    184  1.62        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
    185   1.1   mycroft 		return (EBADF);
    186   1.1   mycroft 
    187  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    188  1.28   thorpej 		error = EBADF;
    189  1.28   thorpej 		goto out;
    190  1.28   thorpej 	}
    191   1.1   mycroft 
    192  1.26  augustss 	com = SCARG(uap, com);
    193  1.65  christos 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
    194   1.6  augustss 
    195   1.1   mycroft 	retval[0] = 0;
    196   1.1   mycroft 
    197  1.26  augustss 	ioctlf = fp->f_ops->fo_ioctl;
    198   1.1   mycroft 	switch (com) {
    199   1.6  augustss 	case OSS_SNDCTL_DSP_RESET:
    200  1.62        ad 		error = ioctlf(fp, AUDIO_FLUSH, NULL);
    201  1.65  christos 		if (error) {
    202  1.65  christos 			DPRINTF(("%s: AUDIO_FLUSH %d\n", __func__, error));
    203  1.28   thorpej 			goto out;
    204  1.65  christos 		}
    205   1.1   mycroft 		break;
    206   1.6  augustss 	case OSS_SNDCTL_DSP_SYNC:
    207  1.62        ad 		error = ioctlf(fp, AUDIO_DRAIN, NULL);
    208  1.65  christos 		if (error) {
    209  1.65  christos 			DPRINTF(("%s: AUDIO_DRAIN %d\n", __func__, error));
    210  1.28   thorpej 			goto out;
    211  1.65  christos 		}
    212  1.39   mycroft 		break;
    213  1.39   mycroft 	case OSS_SNDCTL_DSP_POST:
    214  1.39   mycroft 		/* This call is merely advisory, and may be a nop. */
    215   1.1   mycroft 		break;
    216   1.6  augustss 	case OSS_SNDCTL_DSP_SPEED:
    217  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    218   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    219  1.65  christos 		if (error) {
    220  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SPEED %d\n",
    221  1.65  christos 			     __func__, error));
    222  1.28   thorpej 			goto out;
    223  1.65  christos 		}
    224   1.1   mycroft 		tmpinfo.play.sample_rate =
    225   1.1   mycroft 		tmpinfo.record.sample_rate = idat;
    226  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_SPEED > %d\n", __func__, idat));
    227  1.62        ad 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    228  1.65  christos 		if (error) {
    229  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SPEED %d = %d\n",
    230  1.65  christos 			     __func__, idat, error));
    231  1.28   thorpej 			goto out;
    232  1.65  christos 		}
    233   1.6  augustss 		/* fall into ... */
    234   1.6  augustss 	case OSS_SOUND_PCM_READ_RATE:
    235  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    236  1.65  christos 		if (error) {
    237  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    238  1.65  christos 			 __func__, error));
    239  1.28   thorpej 			goto out;
    240  1.65  christos 		}
    241   1.1   mycroft 		idat = tmpinfo.play.sample_rate;
    242  1.65  christos 		DPRINTF(("%s: SNDCTL_PCM_READ_RATE < %d\n", __func__, idat));
    243   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    244  1.65  christos 		if (error) {
    245  1.65  christos 			DPRINTF(("%s: SOUND_PCM_READ_RATE %d = %d\n",
    246  1.65  christos 			     __func__, idat, error));
    247  1.28   thorpej 			goto out;
    248  1.65  christos 		}
    249   1.1   mycroft 		break;
    250   1.6  augustss 	case OSS_SNDCTL_DSP_STEREO:
    251  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    252   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    253  1.65  christos 		if (error) {
    254  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_STEREO %d\n",
    255  1.65  christos 			     __func__, error));
    256  1.28   thorpej 			goto out;
    257  1.65  christos 		}
    258   1.1   mycroft 		tmpinfo.play.channels =
    259   1.1   mycroft 		tmpinfo.record.channels = idat ? 2 : 1;
    260  1.65  christos 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    261  1.65  christos 		if (error) {
    262  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    263  1.65  christos 			     __func__, error));
    264  1.65  christos 			goto out;
    265  1.65  christos 		}
    266  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    267  1.65  christos 		if (error) {
    268  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    269  1.65  christos 			     __func__, error));
    270  1.28   thorpej 			goto out;
    271  1.65  christos 		}
    272   1.1   mycroft 		idat = tmpinfo.play.channels - 1;
    273   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    274  1.65  christos 		if (error) {
    275  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_STEREO %d = %d\n",
    276  1.65  christos 			     __func__, idat, error));
    277  1.28   thorpej 			goto out;
    278  1.65  christos 		}
    279   1.1   mycroft 		break;
    280   1.6  augustss 	case OSS_SNDCTL_DSP_GETBLKSIZE:
    281  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    282  1.65  christos 		if (error) {
    283  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    284  1.65  christos 			     __func__, error));
    285  1.28   thorpej 			goto out;
    286  1.65  christos 		}
    287  1.62        ad 		setblocksize(fp, &tmpinfo);
    288   1.1   mycroft 		idat = tmpinfo.blocksize;
    289  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE < %d\n",
    290  1.65  christos 		     __func__, idat));
    291   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    292  1.65  christos 		if (error) {
    293  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE %d = %d\n",
    294  1.65  christos 			     __func__, idat, error));
    295  1.28   thorpej 			goto out;
    296  1.65  christos 		}
    297   1.1   mycroft 		break;
    298   1.6  augustss 	case OSS_SNDCTL_DSP_SETFMT:
    299  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    300   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    301  1.65  christos 		if (error) {
    302  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SETFMT %d\n",
    303  1.65  christos 			     __func__, error));
    304  1.28   thorpej 			goto out;
    305  1.65  christos 		}
    306   1.1   mycroft 		switch (idat) {
    307   1.6  augustss 		case OSS_AFMT_MU_LAW:
    308   1.1   mycroft 			tmpinfo.play.precision =
    309   1.1   mycroft 			tmpinfo.record.precision = 8;
    310   1.1   mycroft 			tmpinfo.play.encoding =
    311   1.1   mycroft 			tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
    312   1.1   mycroft 			break;
    313   1.6  augustss 		case OSS_AFMT_A_LAW:
    314   1.1   mycroft 			tmpinfo.play.precision =
    315   1.1   mycroft 			tmpinfo.record.precision = 8;
    316   1.1   mycroft 			tmpinfo.play.encoding =
    317   1.1   mycroft 			tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
    318   1.1   mycroft 			break;
    319   1.6  augustss 		case OSS_AFMT_U8:
    320   1.1   mycroft 			tmpinfo.play.precision =
    321   1.1   mycroft 			tmpinfo.record.precision = 8;
    322   1.1   mycroft 			tmpinfo.play.encoding =
    323   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
    324   1.8  augustss 			break;
    325   1.8  augustss 		case OSS_AFMT_S8:
    326   1.8  augustss 			tmpinfo.play.precision =
    327   1.8  augustss 			tmpinfo.record.precision = 8;
    328   1.8  augustss 			tmpinfo.play.encoding =
    329  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
    330   1.1   mycroft 			break;
    331   1.6  augustss 		case OSS_AFMT_S16_LE:
    332   1.1   mycroft 			tmpinfo.play.precision =
    333   1.1   mycroft 			tmpinfo.record.precision = 16;
    334   1.1   mycroft 			tmpinfo.play.encoding =
    335  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
    336   1.8  augustss 			break;
    337   1.8  augustss 		case OSS_AFMT_S16_BE:
    338   1.8  augustss 			tmpinfo.play.precision =
    339   1.8  augustss 			tmpinfo.record.precision = 16;
    340   1.8  augustss 			tmpinfo.play.encoding =
    341  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
    342   1.8  augustss 			break;
    343   1.8  augustss 		case OSS_AFMT_U16_LE:
    344   1.8  augustss 			tmpinfo.play.precision =
    345   1.8  augustss 			tmpinfo.record.precision = 16;
    346   1.8  augustss 			tmpinfo.play.encoding =
    347   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
    348   1.8  augustss 			break;
    349   1.8  augustss 		case OSS_AFMT_U16_BE:
    350   1.8  augustss 			tmpinfo.play.precision =
    351   1.8  augustss 			tmpinfo.record.precision = 16;
    352   1.8  augustss 			tmpinfo.play.encoding =
    353   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
    354   1.1   mycroft 			break;
    355   1.1   mycroft 		default:
    356  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SETFMT bad fmt %d\n",
    357  1.65  christos 			     __func__, idat));
    358  1.28   thorpej 			error = EINVAL;
    359  1.28   thorpej 			goto out;
    360   1.1   mycroft 		}
    361  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_SETFMT > 0x%x\n",
    362  1.65  christos 		    __func__, idat));
    363  1.65  christos 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    364  1.65  christos 		if (error) {
    365  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    366  1.65  christos 			     __func__, error));
    367  1.65  christos 			goto out;
    368  1.65  christos 		}
    369   1.6  augustss 		/* fall into ... */
    370   1.6  augustss 	case OSS_SOUND_PCM_READ_BITS:
    371  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    372  1.65  christos 		if (error) {
    373  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    374  1.65  christos 			     __func__, error));
    375  1.28   thorpej 			goto out;
    376  1.65  christos 		}
    377   1.5   mycroft 		switch (tmpinfo.play.encoding) {
    378   1.5   mycroft 		case AUDIO_ENCODING_ULAW:
    379   1.6  augustss 			idat = OSS_AFMT_MU_LAW;
    380   1.5   mycroft 			break;
    381   1.5   mycroft 		case AUDIO_ENCODING_ALAW:
    382   1.6  augustss 			idat = OSS_AFMT_A_LAW;
    383   1.5   mycroft 			break;
    384  1.12  augustss 		case AUDIO_ENCODING_SLINEAR_LE:
    385   1.8  augustss 			if (tmpinfo.play.precision == 16)
    386   1.8  augustss 				idat = OSS_AFMT_S16_LE;
    387   1.8  augustss 			else
    388   1.8  augustss 				idat = OSS_AFMT_S8;
    389   1.8  augustss 			break;
    390  1.12  augustss 		case AUDIO_ENCODING_SLINEAR_BE:
    391   1.8  augustss 			if (tmpinfo.play.precision == 16)
    392   1.8  augustss 				idat = OSS_AFMT_S16_BE;
    393   1.8  augustss 			else
    394   1.8  augustss 				idat = OSS_AFMT_S8;
    395   1.8  augustss 			break;
    396   1.8  augustss 		case AUDIO_ENCODING_ULINEAR_LE:
    397   1.8  augustss 			if (tmpinfo.play.precision == 16)
    398   1.8  augustss 				idat = OSS_AFMT_U16_LE;
    399   1.8  augustss 			else
    400   1.8  augustss 				idat = OSS_AFMT_U8;
    401   1.8  augustss 			break;
    402   1.8  augustss 		case AUDIO_ENCODING_ULINEAR_BE:
    403   1.8  augustss 			if (tmpinfo.play.precision == 16)
    404   1.8  augustss 				idat = OSS_AFMT_U16_BE;
    405   1.8  augustss 			else
    406   1.8  augustss 				idat = OSS_AFMT_U8;
    407   1.5   mycroft 			break;
    408   1.5   mycroft 		case AUDIO_ENCODING_ADPCM:
    409   1.6  augustss 			idat = OSS_AFMT_IMA_ADPCM;
    410   1.5   mycroft 			break;
    411  1.65  christos 		default:
    412  1.65  christos 			DPRINTF(("%s: SOUND_PCM_READ_BITS bad encoding %d\n",
    413  1.65  christos 			     __func__, tmpinfo.play.encoding));
    414  1.65  christos 			error = EINVAL;
    415  1.65  christos 			goto out;
    416   1.5   mycroft 		}
    417  1.65  christos 		DPRINTF(("%s: SOUND_PCM_READ_BITS < 0x%x\n",
    418  1.65  christos 		    __func__, idat));
    419   1.5   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    420  1.65  christos 		if (error) {
    421  1.65  christos 			DPRINTF(("%s: SOUND_PCM_READ_BITS %d = %d\n",
    422  1.65  christos 			     __func__, idat, error));
    423  1.28   thorpej 			goto out;
    424  1.65  christos 		}
    425   1.1   mycroft 		break;
    426   1.6  augustss 	case OSS_SNDCTL_DSP_CHANNELS:
    427  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    428   1.6  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    429  1.65  christos 		if (error) {
    430  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_CHANNELS %d\n",
    431  1.65  christos 			     __func__, error));
    432  1.28   thorpej 			goto out;
    433  1.65  christos 		}
    434   1.6  augustss 		tmpinfo.play.channels =
    435   1.6  augustss 		tmpinfo.record.channels = idat;
    436  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_CHANNELS > %d\n", __func__, idat));
    437  1.65  christos 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    438  1.65  christos 		if (error) {
    439  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    440  1.65  christos 			     __func__, error));
    441  1.65  christos 			goto out;
    442  1.65  christos 		}
    443   1.6  augustss 		/* fall into ... */
    444   1.6  augustss 	case OSS_SOUND_PCM_READ_CHANNELS:
    445  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    446  1.65  christos 		if (error) {
    447  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    448  1.65  christos 			     __func__, error));
    449  1.28   thorpej 			goto out;
    450  1.65  christos 		}
    451   1.6  augustss 		idat = tmpinfo.play.channels;
    452  1.65  christos 		DPRINTF(("%s: SOUND_PCM_READ_CHANNELS < %d\n", __func__, idat));
    453   1.6  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    454  1.65  christos 		if (error) {
    455  1.65  christos 			DPRINTF(("%s: SOUND_PCM_READ_CHANNELS %d = %d\n",
    456  1.65  christos 			     __func__, idat, error));
    457  1.28   thorpej 			goto out;
    458  1.65  christos 		}
    459   1.6  augustss 		break;
    460   1.6  augustss 	case OSS_SOUND_PCM_WRITE_FILTER:
    461   1.6  augustss 	case OSS_SOUND_PCM_READ_FILTER:
    462  1.28   thorpej 		error = EINVAL; /* XXX unimplemented */
    463  1.65  christos 		DPRINTF(("%s: SOUND_PCM_{READ,WRITE}_FILTER filter\n",
    464  1.65  christos 		     __func__));
    465  1.28   thorpej 		goto out;
    466   1.6  augustss 	case OSS_SNDCTL_DSP_SUBDIVIDE:
    467   1.9  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    468  1.65  christos 		if (error) {
    469  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d\n",
    470  1.65  christos 			     __func__, error));
    471  1.28   thorpej 			goto out;
    472  1.65  christos 		}
    473  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    474  1.65  christos 		if (error) {
    475  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    476  1.65  christos 			     __func__, error));
    477  1.65  christos 			goto out;
    478  1.65  christos 		}
    479  1.62        ad 		setblocksize(fp, &tmpinfo);
    480   1.9  augustss 		if (idat == 0)
    481  1.23  augustss 			idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
    482  1.23  augustss 		idat = (tmpinfo.play.buffer_size / idat) & -4;
    483  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    484   1.9  augustss 		tmpinfo.blocksize = idat;
    485  1.62        ad 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    486  1.65  christos 		if (error) {
    487  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    488  1.65  christos 			     __func__, error));
    489  1.28   thorpej 			goto out;
    490  1.65  christos 		}
    491  1.23  augustss 		idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
    492   1.9  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    493  1.65  christos 		if (error) {
    494  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d = %d\n",
    495  1.65  christos 			     __func__, idat, error));
    496  1.28   thorpej 			goto out;
    497  1.65  christos 		}
    498   1.9  augustss 		break;
    499   1.6  augustss 	case OSS_SNDCTL_DSP_SETFRAGMENT:
    500  1.50   xtraeme 		AUDIO_INITINFO(&tmpinfo);
    501   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    502  1.65  christos 		if (error) {
    503  1.65  christos 			DPRINTF(("%s: DSP_SETFRAGMENT %d\n",
    504  1.65  christos 			     __func__, error));
    505  1.28   thorpej 			goto out;
    506  1.65  christos 		}
    507  1.28   thorpej 		if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
    508  1.65  christos 			DPRINTF(("%s: DSP_SETFRAGMENT bad ival%d\n",
    509  1.65  christos 			     __func__, idat));
    510  1.28   thorpej 			error = EINVAL;
    511  1.28   thorpej 			goto out;
    512  1.28   thorpej 		}
    513   1.1   mycroft 		tmpinfo.blocksize = 1 << (idat & 0xffff);
    514  1.24   mycroft 		tmpinfo.hiwat = (idat >> 16) & 0x7fff;
    515  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT blksize=%d, "
    516  1.65  christos 		    "hiwat=%d\n", __func__, tmpinfo.blocksize, tmpinfo.hiwat));
    517  1.21  augustss 		if (tmpinfo.hiwat == 0)	/* 0 means set to max */
    518  1.21  augustss 			tmpinfo.hiwat = 65536;
    519  1.65  christos 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    520  1.65  christos 		if (error) {
    521  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    522  1.65  christos 			     __func__, error));
    523  1.65  christos 			goto out;
    524  1.65  christos 		}
    525  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    526  1.65  christos 		if (error) {
    527  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    528  1.65  christos 			     __func__, error));
    529  1.28   thorpej 			goto out;
    530  1.65  christos 		}
    531  1.21  augustss 		u = tmpinfo.blocksize;
    532  1.25  augustss 		for(idat = 0; u > 1; idat++, u >>= 1)
    533  1.21  augustss 			;
    534  1.24   mycroft 		idat |= (tmpinfo.hiwat & 0x7fff) << 16;
    535   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    536  1.65  christos 		if (error) {
    537  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT  %d = %d\n",
    538  1.65  christos 			     __func__, idat, error));
    539  1.28   thorpej 			goto out;
    540  1.65  christos 		}
    541   1.1   mycroft 		break;
    542   1.6  augustss 	case OSS_SNDCTL_DSP_GETFMTS:
    543  1.65  christos 		for (idat = 0, tmpenc.index = 0;
    544  1.62        ad 		    ioctlf(fp, AUDIO_GETENC, &tmpenc) == 0;
    545  1.16  augustss 		    tmpenc.index++) {
    546  1.16  augustss 			switch(tmpenc.encoding) {
    547  1.16  augustss 			case AUDIO_ENCODING_ULAW:
    548  1.16  augustss 				idat |= OSS_AFMT_MU_LAW;
    549  1.16  augustss 				break;
    550  1.16  augustss 			case AUDIO_ENCODING_ALAW:
    551  1.16  augustss 				idat |= OSS_AFMT_A_LAW;
    552  1.16  augustss 				break;
    553  1.16  augustss 			case AUDIO_ENCODING_SLINEAR:
    554  1.16  augustss 				idat |= OSS_AFMT_S8;
    555  1.16  augustss 				break;
    556  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_LE:
    557  1.16  augustss 				if (tmpenc.precision == 16)
    558  1.16  augustss 					idat |= OSS_AFMT_S16_LE;
    559  1.16  augustss 				else
    560  1.16  augustss 					idat |= OSS_AFMT_S8;
    561  1.16  augustss 				break;
    562  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_BE:
    563  1.16  augustss 				if (tmpenc.precision == 16)
    564  1.16  augustss 					idat |= OSS_AFMT_S16_BE;
    565  1.16  augustss 				else
    566  1.16  augustss 					idat |= OSS_AFMT_S8;
    567  1.16  augustss 				break;
    568  1.16  augustss 			case AUDIO_ENCODING_ULINEAR:
    569  1.16  augustss 				idat |= OSS_AFMT_U8;
    570  1.16  augustss 				break;
    571  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_LE:
    572  1.16  augustss 				if (tmpenc.precision == 16)
    573  1.16  augustss 					idat |= OSS_AFMT_U16_LE;
    574  1.16  augustss 				else
    575  1.16  augustss 					idat |= OSS_AFMT_U8;
    576  1.16  augustss 				break;
    577  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_BE:
    578  1.16  augustss 				if (tmpenc.precision == 16)
    579  1.16  augustss 					idat |= OSS_AFMT_U16_BE;
    580  1.16  augustss 				else
    581  1.16  augustss 					idat |= OSS_AFMT_U8;
    582  1.16  augustss 				break;
    583  1.16  augustss 			case AUDIO_ENCODING_ADPCM:
    584  1.16  augustss 				idat |= OSS_AFMT_IMA_ADPCM;
    585  1.16  augustss 				break;
    586  1.16  augustss 			default:
    587  1.65  christos 				DPRINTF(("%s: SNDCTL_DSP_GETFMTS unknown %d\n",
    588  1.65  christos 				    __func__, tmpenc.encoding));
    589  1.16  augustss 				break;
    590  1.16  augustss 			}
    591  1.16  augustss 		}
    592  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_GETFMTS < 0x%x\n",
    593  1.65  christos 		    __func__, idat));
    594   1.6  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    595  1.65  christos 		if (error) {
    596  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETFMTS = %x = %d\n",
    597  1.65  christos 			    __func__, idat, error));
    598  1.28   thorpej 			goto out;
    599  1.65  christos 		}
    600   1.6  augustss 		break;
    601   1.6  augustss 	case OSS_SNDCTL_DSP_GETOSPACE:
    602  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    603  1.65  christos 		if (error) {
    604  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    605  1.65  christos 			     __func__, error));
    606  1.35  augustss 			goto out;
    607  1.65  christos 		}
    608  1.62        ad 		setblocksize(fp, &tmpinfo);
    609  1.35  augustss 		bufinfo.fragsize = tmpinfo.blocksize;
    610  1.35  augustss 		bufinfo.fragments = tmpinfo.hiwat -
    611  1.35  augustss 		    (tmpinfo.play.seek + tmpinfo.blocksize - 1) /
    612  1.35  augustss 		    tmpinfo.blocksize;
    613  1.35  augustss 		bufinfo.fragstotal = tmpinfo.hiwat;
    614  1.35  augustss 		bufinfo.bytes =
    615  1.35  augustss 		    tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
    616  1.35  augustss 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
    617  1.65  christos 		if (error) {
    618  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETOSPACE = %d\n",
    619  1.65  christos 			    __func__, error));
    620  1.35  augustss 			goto out;
    621  1.65  christos 		}
    622  1.35  augustss 		break;
    623  1.14  augustss 	case OSS_SNDCTL_DSP_GETISPACE:
    624  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    625  1.65  christos 		if (error) {
    626  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    627  1.65  christos 			     __func__, error));
    628  1.28   thorpej 			goto out;
    629  1.65  christos 		}
    630  1.62        ad 		setblocksize(fp, &tmpinfo);
    631  1.13  augustss 		bufinfo.fragsize = tmpinfo.blocksize;
    632  1.35  augustss 		bufinfo.fragments = tmpinfo.hiwat -
    633  1.35  augustss 		    (tmpinfo.record.seek + tmpinfo.blocksize - 1) /
    634  1.35  augustss 		    tmpinfo.blocksize;
    635  1.35  augustss                 bufinfo.fragstotal = tmpinfo.hiwat;
    636  1.35  augustss 		bufinfo.bytes =
    637  1.35  augustss 		    tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.record.seek;
    638  1.13  augustss 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
    639  1.65  christos 		if (error) {
    640  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETISPACE %d %d %d %d = %d\n",
    641  1.65  christos 			     __func__, bufinfo.fragsize, bufinfo.fragments,
    642  1.65  christos 			     bufinfo.fragstotal, bufinfo.bytes, error));
    643  1.28   thorpej 			goto out;
    644  1.65  christos 		}
    645  1.13  augustss 		break;
    646   1.6  augustss 	case OSS_SNDCTL_DSP_NONBLOCK:
    647  1.18  augustss 		idat = 1;
    648  1.62        ad 		error = ioctlf(fp, FIONBIO, &idat);
    649  1.65  christos 		if (error) {
    650  1.65  christos 			DPRINTF(("%s: SENDCLT_DSP_NONBLOCK %d\n",
    651  1.65  christos 			     __func__, error));
    652  1.28   thorpej 			goto out;
    653  1.65  christos 		}
    654  1.18  augustss 		break;
    655   1.6  augustss 	case OSS_SNDCTL_DSP_GETCAPS:
    656  1.62        ad 		error = ioctlf(fp, AUDIO_GETPROPS, &idata);
    657  1.65  christos 		if (error) {
    658  1.65  christos 			DPRINTF(("%s: AUDIO_GETPROPS %d\n",
    659  1.65  christos 			     __func__, error));
    660  1.28   thorpej 			goto out;
    661  1.65  christos 		}
    662  1.13  augustss 		idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
    663  1.13  augustss 		if (idata & AUDIO_PROP_FULLDUPLEX)
    664  1.13  augustss 			idat |= OSS_DSP_CAP_DUPLEX;
    665  1.13  augustss 		if (idata & AUDIO_PROP_MMAP)
    666  1.13  augustss 			idat |= OSS_DSP_CAP_MMAP;
    667  1.65  christos 		DPRINTF(("%s: SNDCL_DSP_GETCAPS %s duplex, %smmap\n",
    668  1.65  christos 		     __func__, (idat & OSS_DSP_CAP_DUPLEX) ? "full" : "half",
    669  1.65  christos 		     (idat & OSS_DSP_CAP_MMAP) ? "" : "no "));
    670  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    671  1.65  christos 		if (error) {
    672  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETCAPS %x = %d\n", __func__,
    673  1.65  christos 			    idat, error));
    674  1.28   thorpej 			goto out;
    675  1.65  christos 		}
    676  1.13  augustss 		break;
    677  1.13  augustss #if 0
    678  1.13  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    679  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    680  1.65  christos 		if (error) {
    681  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    682  1.65  christos 			     __func__, error));
    683  1.28   thorpej 			goto out;
    684  1.65  christos 		}
    685  1.13  augustss 		idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
    686  1.13  augustss 		       (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
    687  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    688  1.65  christos 		if (error) {
    689  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SETRIGGER %x = %d\n",
    690  1.65  christos 			    __func__, idat, error));
    691  1.28   thorpej 			goto out;
    692  1.65  christos 		}
    693  1.13  augustss 		break;
    694  1.13  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    695  1.65  christos 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo, p);
    696  1.65  christos 		if (error) {
    697  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    698  1.65  christos 			     __func__, error));
    699  1.65  christos 			goto out;
    700  1.65  christos 		}
    701  1.13  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    702  1.65  christos 		if (error) {
    703  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    704  1.65  christos 			     __func__, error));
    705  1.28   thorpej 			goto out;
    706  1.65  christos 		}
    707  1.13  augustss 		tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
    708  1.13  augustss 		tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
    709  1.65  christos 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
    710  1.65  christos 		if (error) {
    711  1.65  christos 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
    712  1.65  christos 			     __func__, error));
    713  1.65  christos 			goto out;
    714  1.65  christos 		}
    715   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    716  1.65  christos 		if (error) {
    717  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_SETRIGGER %x = %d\n",
    718  1.65  christos 			    __func__, idat, error));
    719  1.28   thorpej 			goto out;
    720  1.65  christos 		}
    721   1.1   mycroft 		break;
    722  1.13  augustss #else
    723   1.6  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    724   1.6  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    725  1.13  augustss 		/* XXX Do nothing for now. */
    726  1.13  augustss 		idat = OSS_PCM_ENABLE_OUTPUT;
    727  1.28   thorpej 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    728  1.65  christos 		if (error) {
    729  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_{GET,SET}RIGGER %x = %d\n",
    730  1.65  christos 			    __func__, idat, error));
    731  1.65  christos 			goto out;
    732  1.65  christos 		}
    733  1.65  christos 		break;
    734  1.13  augustss #endif
    735   1.6  augustss 	case OSS_SNDCTL_DSP_GETIPTR:
    736  1.62        ad 		error = ioctlf(fp, AUDIO_GETIOFFS, &tmpoffs);
    737  1.65  christos 		if (error) {
    738  1.65  christos 			DPRINTF(("%s: AUDIO_GETIOFFS %d\n",
    739  1.65  christos 			     __func__, error));
    740  1.28   thorpej 			goto out;
    741  1.65  christos 		}
    742  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    743  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    744  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    745  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    746  1.65  christos 		if (error) {
    747  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETIPTR %d\n",
    748  1.65  christos 			    __func__, error));
    749  1.28   thorpej 			goto out;
    750  1.65  christos 		}
    751  1.13  augustss 		break;
    752   1.6  augustss 	case OSS_SNDCTL_DSP_GETOPTR:
    753  1.62        ad 		error = ioctlf(fp, AUDIO_GETOOFFS, &tmpoffs);
    754  1.65  christos 		if (error) {
    755  1.65  christos 			DPRINTF(("%s: AUDIO_GETOOFFS %d\n",
    756  1.65  christos 			     __func__, error));
    757  1.28   thorpej 			goto out;
    758  1.65  christos 		}
    759  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    760  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    761  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    762  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    763  1.65  christos 		if (error) {
    764  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETOPTR %d\n",
    765  1.65  christos 			    __func__, error));
    766  1.28   thorpej 			goto out;
    767  1.65  christos 		}
    768  1.13  augustss 		break;
    769  1.40  jdolecek 	case OSS_SNDCTL_DSP_SETDUPLEX:
    770  1.40  jdolecek 		idat = 1;
    771  1.62        ad 		error = ioctlf(fp, AUDIO_SETFD, &idat);
    772  1.65  christos 		if (error) {
    773  1.65  christos 			DPRINTF(("%s: AUDIO_SETFD %d = %d\n",
    774  1.65  christos 			    __func__, idat, error));
    775  1.65  christos 			goto out;
    776  1.65  christos 		}
    777  1.65  christos 		break;
    778  1.65  christos 	case OSS_SNDCTL_DSP_MAPINBUF:
    779  1.65  christos 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPINBUF\n",
    780  1.65  christos 		    __func__));
    781  1.65  christos 		error = EINVAL;
    782  1.40  jdolecek 		goto out;
    783   1.6  augustss 	case OSS_SNDCTL_DSP_MAPOUTBUF:
    784  1.65  christos 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPOUTBUF\n",
    785  1.65  christos 		    __func__));
    786  1.65  christos 		error = EINVAL;
    787  1.65  christos 		goto out;
    788   1.6  augustss 	case OSS_SNDCTL_DSP_SETSYNCRO:
    789  1.65  christos 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_GETSYNCHRO\n",
    790  1.65  christos 		    __func__));
    791  1.28   thorpej 		error = EINVAL;
    792  1.28   thorpej 		goto out;
    793  1.57   mlelstv 	case OSS_SNDCTL_DSP_GETODELAY:
    794  1.62        ad 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
    795  1.65  christos 		if (error) {
    796  1.65  christos 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
    797  1.65  christos 			    __func__, error));
    798  1.57   mlelstv 			goto out;
    799  1.65  christos 		}
    800  1.57   mlelstv 		idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
    801  1.57   mlelstv 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    802  1.65  christos 		if (error) {
    803  1.65  christos 			DPRINTF(("%s: SNDCTL_DSP_GETODELAY %d\n",
    804  1.65  christos 			    __func__, error));
    805  1.57   mlelstv 			goto out;
    806  1.65  christos 		}
    807  1.57   mlelstv 		break;
    808  1.57   mlelstv 	case OSS_SNDCTL_DSP_PROFILE:
    809  1.57   mlelstv 		/* This gives just a hint to the driver,
    810  1.57   mlelstv 		 * implementing it as a NOP is ok
    811  1.57   mlelstv 		 */
    812  1.65  christos 		DPRINTF(("%s: SNDCTL_DSP_PROFILE\n", __func__));
    813  1.57   mlelstv 		break;
    814   1.1   mycroft 	default:
    815  1.65  christos 		DPRINTF(("%s: Unimplemented 0x%lx\n", __func__, com));
    816  1.28   thorpej 		error = EINVAL;
    817  1.28   thorpej 		goto out;
    818   1.1   mycroft 	}
    819   1.1   mycroft 
    820  1.28   thorpej  out:
    821  1.62        ad  	fd_putfile(SCARG(uap, fd));
    822  1.28   thorpej 	return error;
    823   1.3   mycroft }
    824   1.3   mycroft 
    825   1.6  augustss /* If the NetBSD mixer device should have more than 32 devices
    826   1.6  augustss  * some will not be available to Linux */
    827  1.20  augustss #define NETBSD_MAXDEVS 64
    828   1.6  augustss struct audiodevinfo {
    829   1.6  augustss 	int done;
    830   1.6  augustss 	dev_t dev;
    831  1.47     perry 	int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
    832   1.7  augustss 	        rdevmap[NETBSD_MAXDEVS];
    833  1.31  augustss 	char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
    834  1.31  augustss 	int enum2opaque[NETBSD_MAXDEVS];
    835   1.6  augustss         u_long devmask, recmask, stereomask;
    836   1.6  augustss 	u_long caps, source;
    837   1.6  augustss };
    838   1.6  augustss 
    839  1.31  augustss static int
    840  1.31  augustss opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
    841  1.31  augustss {
    842  1.31  augustss 	int i, o;
    843  1.31  augustss 
    844  1.31  augustss 	for (i = 0; i < NETBSD_MAXDEVS; i++) {
    845  1.31  augustss 		o = di->enum2opaque[i];
    846  1.31  augustss 		if (o == opq)
    847  1.31  augustss 			break;
    848  1.31  augustss 		if (o == -1 && label != NULL &&
    849  1.31  augustss 		    !strncmp(di->names[i], label->name, sizeof di->names[i])) {
    850  1.31  augustss 			di->enum2opaque[i] = opq;
    851  1.31  augustss 			break;
    852  1.31  augustss 		}
    853  1.31  augustss 	}
    854  1.31  augustss 	if (i >= NETBSD_MAXDEVS)
    855  1.31  augustss 		i = -1;
    856  1.31  augustss 	/*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
    857  1.31  augustss 	return (i);
    858  1.31  augustss }
    859  1.31  augustss 
    860  1.31  augustss static int
    861  1.31  augustss enum_to_ord(struct audiodevinfo *di, int enm)
    862  1.31  augustss {
    863  1.31  augustss 	if (enm >= NETBSD_MAXDEVS)
    864  1.31  augustss 		return (-1);
    865  1.31  augustss 
    866  1.31  augustss 	/*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
    867  1.31  augustss 	return (di->enum2opaque[enm]);
    868  1.31  augustss }
    869  1.31  augustss 
    870  1.31  augustss static int
    871  1.31  augustss enum_to_mask(struct audiodevinfo *di, int enm)
    872  1.31  augustss {
    873  1.31  augustss 	int m;
    874  1.31  augustss 	if (enm >= NETBSD_MAXDEVS)
    875  1.31  augustss 		return (0);
    876  1.31  augustss 
    877  1.31  augustss 	m = di->enum2opaque[enm];
    878  1.31  augustss 	if (m == -1)
    879  1.31  augustss 		m = 0;
    880  1.31  augustss 	/*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
    881  1.31  augustss 	return (m);
    882  1.31  augustss }
    883  1.31  augustss 
    884  1.47     perry /*
    885   1.6  augustss  * Collect the audio device information to allow faster
    886   1.6  augustss  * emulation of the Linux mixer ioctls.  Cache the information
    887   1.6  augustss  * to eliminate the overhead of repeating all the ioctls needed
    888   1.6  augustss  * to collect the information.
    889   1.6  augustss  */
    890   1.6  augustss static struct audiodevinfo *
    891  1.62        ad getdevinfo(file_t *fp)
    892   1.6  augustss {
    893   1.6  augustss 	mixer_devinfo_t mi;
    894  1.31  augustss 	int i, j, e;
    895  1.33  jdolecek 	static const struct {
    896  1.33  jdolecek 		const char *name;
    897   1.6  augustss 		int code;
    898   1.6  augustss 	} *dp, devs[] = {
    899   1.6  augustss 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
    900   1.6  augustss 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
    901   1.6  augustss 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
    902   1.6  augustss 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
    903  1.37       kim 		{ AudioNaux,		OSS_SOUND_MIXER_LINE1 },
    904   1.6  augustss 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
    905  1.15  augustss 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
    906   1.6  augustss 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
    907   1.6  augustss 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
    908   1.6  augustss 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
    909   1.6  augustss /*		{ AudioNheadphone,	?? },*/
    910   1.6  augustss 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
    911   1.6  augustss 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
    912  1.15  augustss /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
    913   1.6  augustss /*		{ AudioNstereo,		?? },*/
    914   1.6  augustss /*		{ AudioNmono,		?? },*/
    915   1.6  augustss 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
    916   1.6  augustss /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
    917  1.10  augustss 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
    918   1.6  augustss /*		{ AudioNmixerout,	?? },*/
    919   1.6  augustss 		{ 0, -1 }
    920   1.6  augustss 	};
    921  1.62        ad 	int (*ioctlf)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
    922   1.6  augustss 	struct vnode *vp;
    923   1.6  augustss 	struct vattr va;
    924  1.53  christos 	static struct audiodevinfo devcache;
    925   1.6  augustss 	struct audiodevinfo *di = &devcache;
    926  1.46      kent 	int mlen, dlen;
    927   1.6  augustss 
    928  1.47     perry 	/*
    929  1.31  augustss 	 * Figure out what device it is so we can check if the
    930   1.6  augustss 	 * cached data is valid.
    931   1.6  augustss 	 */
    932  1.62        ad 	vp = fp->f_data;
    933   1.6  augustss 	if (vp->v_type != VCHR)
    934   1.6  augustss 		return 0;
    935  1.62        ad 	if (VOP_GETATTR(vp, &va, kauth_cred_get()))
    936   1.6  augustss 		return 0;
    937   1.6  augustss 	if (di->done && di->dev == va.va_rdev)
    938   1.6  augustss 		return di;
    939   1.6  augustss 
    940   1.6  augustss 	di->done = 1;
    941   1.6  augustss 	di->dev = va.va_rdev;
    942   1.6  augustss 	di->devmask = 0;
    943   1.6  augustss 	di->recmask = 0;
    944   1.6  augustss 	di->stereomask = 0;
    945  1.31  augustss 	di->source = ~0;
    946   1.6  augustss 	di->caps = 0;
    947   1.6  augustss 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
    948   1.6  augustss 		di->devmap[i] = -1;
    949  1.31  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
    950   1.6  augustss 		di->rdevmap[i] = -1;
    951  1.31  augustss 		di->names[i][0] = '\0';
    952  1.31  augustss 		di->enum2opaque[i] = -1;
    953  1.31  augustss 	}
    954   1.6  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
    955   1.6  augustss 		mi.index = i;
    956  1.62        ad 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) < 0)
    957   1.6  augustss 			break;
    958   1.6  augustss 		switch(mi.type) {
    959   1.6  augustss 		case AUDIO_MIXER_VALUE:
    960  1.46      kent 			for(dp = devs; dp->name; dp++) {
    961  1.46      kent 				if (strcmp(dp->name, mi.label.name) == 0)
    962   1.6  augustss 					break;
    963  1.46      kent 				dlen = strlen(dp->name);
    964  1.46      kent 				mlen = strlen(mi.label.name);
    965  1.46      kent 				if (dlen < mlen
    966  1.46      kent 				    && mi.label.name[mlen-dlen-1] == '.'
    967  1.46      kent 				    && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
    968  1.46      kent 					break;
    969  1.46      kent 			}
    970   1.6  augustss 			if (dp->code >= 0) {
    971   1.6  augustss 				di->devmap[dp->code] = i;
    972   1.6  augustss 				di->rdevmap[i] = dp->code;
    973   1.6  augustss 				di->devmask |= 1 << dp->code;
    974   1.6  augustss 				if (mi.un.v.num_channels == 2)
    975   1.6  augustss 					di->stereomask |= 1 << dp->code;
    976  1.47     perry 				strncpy(di->names[i], mi.label.name,
    977  1.31  augustss 					sizeof di->names[i]);
    978   1.6  augustss 			}
    979   1.6  augustss 			break;
    980  1.31  augustss 		}
    981  1.31  augustss 	}
    982  1.31  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
    983  1.31  augustss 		mi.index = i;
    984  1.62        ad 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) < 0)
    985  1.31  augustss 			break;
    986  1.31  augustss 		if (strcmp(mi.label.name, AudioNsource) != 0)
    987  1.31  augustss 			continue;
    988  1.31  augustss 		di->source = i;
    989  1.31  augustss 		switch(mi.type) {
    990   1.6  augustss 		case AUDIO_MIXER_ENUM:
    991  1.31  augustss 			for(j = 0; j < mi.un.e.num_mem; j++) {
    992  1.31  augustss 				e = opaque_to_enum(di,
    993  1.31  augustss 						   &mi.un.e.member[j].label,
    994  1.31  augustss 						   mi.un.e.member[j].ord);
    995  1.31  augustss 				if (e >= 0)
    996  1.31  augustss 					di->recmask |= 1 << di->rdevmap[e];
    997   1.6  augustss 			}
    998  1.31  augustss 			di->caps = OSS_SOUND_CAP_EXCL_INPUT;
    999   1.6  augustss 			break;
   1000   1.6  augustss 		case AUDIO_MIXER_SET:
   1001  1.31  augustss 			for(j = 0; j < mi.un.s.num_mem; j++) {
   1002  1.31  augustss 				e = opaque_to_enum(di,
   1003  1.31  augustss 						   &mi.un.s.member[j].label,
   1004  1.31  augustss 						   mi.un.s.member[j].mask);
   1005  1.31  augustss 				if (e >= 0)
   1006  1.31  augustss 					di->recmask |= 1 << di->rdevmap[e];
   1007   1.6  augustss 			}
   1008   1.6  augustss 			break;
   1009   1.6  augustss 		}
   1010   1.6  augustss 	}
   1011   1.6  augustss 	return di;
   1012   1.6  augustss }
   1013   1.6  augustss 
   1014   1.6  augustss int
   1015  1.61       dsl oss_ioctl_mixer(struct lwp *lwp, const struct oss_sys_ioctl_args *uap, register_t *retval)
   1016  1.61       dsl {
   1017  1.61       dsl 	/* {
   1018   1.6  augustss 		syscallarg(int) fd;
   1019   1.6  augustss 		syscallarg(u_long) com;
   1020  1.55  christos 		syscallarg(void *) data;
   1021  1.61       dsl 	} */
   1022  1.62        ad 	file_t *fp;
   1023   1.6  augustss 	u_long com;
   1024   1.6  augustss 	struct audiodevinfo *di;
   1025   1.6  augustss 	mixer_ctrl_t mc;
   1026  1.30  augustss 	struct oss_mixer_info omi;
   1027  1.30  augustss 	struct audio_device adev;
   1028   1.6  augustss 	int idat;
   1029   1.6  augustss 	int i;
   1030   1.6  augustss 	int error;
   1031  1.31  augustss 	int l, r, n, e;
   1032  1.62        ad 	int (*ioctlf)(file_t *, u_long, void *);
   1033   1.6  augustss 
   1034  1.62        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
   1035   1.6  augustss 		return (EBADF);
   1036   1.6  augustss 
   1037  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
   1038  1.28   thorpej 		error = EBADF;
   1039  1.28   thorpej 		goto out;
   1040  1.28   thorpej 	}
   1041   1.6  augustss 
   1042   1.6  augustss 	com = SCARG(uap, com);
   1043  1.65  christos 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
   1044  1.26  augustss 
   1045   1.6  augustss 	retval[0] = 0;
   1046   1.6  augustss 
   1047  1.62        ad 	di = getdevinfo(fp);
   1048  1.28   thorpej 	if (di == 0) {
   1049  1.28   thorpej 		error = EINVAL;
   1050  1.28   thorpej 		goto out;
   1051  1.28   thorpej 	}
   1052   1.6  augustss 
   1053   1.6  augustss 	ioctlf = fp->f_ops->fo_ioctl;
   1054   1.6  augustss 	switch (com) {
   1055  1.31  augustss 	case OSS_GET_VERSION:
   1056  1.31  augustss 		idat = OSS_SOUND_VERSION;
   1057  1.31  augustss 		break;
   1058  1.30  augustss 	case OSS_SOUND_MIXER_INFO:
   1059  1.30  augustss 	case OSS_SOUND_OLD_MIXER_INFO:
   1060  1.62        ad 		error = ioctlf(fp, AUDIO_GETDEV, &adev);
   1061  1.65  christos 		if (error) {
   1062  1.65  christos 			DPRINTF(("%s: AUDIO_GETDEV %d\n",
   1063  1.65  christos 			    __func__, error));
   1064  1.43  augustss 			goto out;
   1065  1.65  christos 		}
   1066  1.30  augustss 		omi.modify_counter = 1;
   1067  1.30  augustss 		strncpy(omi.id, adev.name, sizeof omi.id);
   1068  1.30  augustss 		strncpy(omi.name, adev.name, sizeof omi.name);
   1069  1.43  augustss 		error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
   1070  1.65  christos 		if (error) {
   1071  1.65  christos 			DPRINTF(("%s: OSS_SOUND_MIXER_INFO %d\n",
   1072  1.65  christos 			    __func__, error));
   1073  1.65  christos 			goto out;
   1074  1.65  christos 		}
   1075  1.65  christos 		break;
   1076   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECSRC:
   1077  1.28   thorpej 		if (di->source == -1) {
   1078  1.65  christos 			DPRINTF(("%s: OSS_SOUND_MIXER_READ_RECSRC bad source\n",
   1079  1.65  christos 			    __func__));
   1080  1.28   thorpej 			error = EINVAL;
   1081  1.28   thorpej 			goto out;
   1082  1.28   thorpej 		}
   1083   1.6  augustss 		mc.dev = di->source;
   1084   1.6  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
   1085   1.6  augustss 			mc.type = AUDIO_MIXER_ENUM;
   1086  1.62        ad 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
   1087  1.65  christos 			if (error) {
   1088  1.65  christos 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
   1089  1.65  christos 				    __func__, error));
   1090  1.28   thorpej 				goto out;
   1091  1.65  christos 			}
   1092  1.31  augustss 			e = opaque_to_enum(di, NULL, mc.un.ord);
   1093  1.31  augustss 			if (e >= 0)
   1094  1.31  augustss 				idat = 1 << di->rdevmap[e];
   1095   1.6  augustss 		} else {
   1096   1.6  augustss 			mc.type = AUDIO_MIXER_SET;
   1097  1.62        ad 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
   1098  1.65  christos 			if (error) {
   1099  1.65  christos 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
   1100  1.65  christos 				    __func__, error));
   1101  1.28   thorpej 				goto out;
   1102  1.65  christos 			}
   1103  1.31  augustss 			e = opaque_to_enum(di, NULL, mc.un.mask);
   1104  1.31  augustss 			if (e >= 0)
   1105  1.31  augustss 				idat = 1 << di->rdevmap[e];
   1106   1.6  augustss 		}
   1107   1.6  augustss 		break;
   1108   1.6  augustss 	case OSS_SOUND_MIXER_READ_DEVMASK:
   1109   1.6  augustss 		idat = di->devmask;
   1110   1.6  augustss 		break;
   1111   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECMASK:
   1112   1.6  augustss 		idat = di->recmask;
   1113   1.6  augustss 		break;
   1114   1.6  augustss 	case OSS_SOUND_MIXER_READ_STEREODEVS:
   1115   1.6  augustss 		idat = di->stereomask;
   1116   1.6  augustss 		break;
   1117   1.6  augustss 	case OSS_SOUND_MIXER_READ_CAPS:
   1118   1.6  augustss 		idat = di->caps;
   1119   1.6  augustss 		break;
   1120   1.6  augustss 	case OSS_SOUND_MIXER_WRITE_RECSRC:
   1121  1.15  augustss 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
   1122  1.28   thorpej 		if (di->source == -1) {
   1123  1.65  christos 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC bad "
   1124  1.65  christos 			    "source\n", __func__));
   1125  1.28   thorpej 			error = EINVAL;
   1126  1.28   thorpej 			goto out;
   1127  1.28   thorpej 		}
   1128   1.6  augustss 		mc.dev = di->source;
   1129   1.6  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1130  1.65  christos 		if (error) {
   1131  1.65  christos 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC %d\n",
   1132  1.65  christos 			    __func__, error));
   1133  1.28   thorpej 			goto out;
   1134  1.65  christos 		}
   1135   1.7  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
   1136   1.7  augustss 			mc.type = AUDIO_MIXER_ENUM;
   1137   1.7  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
   1138   1.7  augustss 				if (idat & (1 << i))
   1139   1.7  augustss 					break;
   1140   1.7  augustss 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
   1141  1.43  augustss 			    di->devmap[i] == -1) {
   1142  1.43  augustss 				error = EINVAL;
   1143  1.65  christos 				DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC "
   1144  1.65  christos 				    "bad index %d\n", __func__, i));
   1145  1.43  augustss 				goto out;
   1146  1.43  augustss 			}
   1147  1.31  augustss 			mc.un.ord = enum_to_ord(di, di->devmap[i]);
   1148   1.7  augustss 		} else {
   1149   1.7  augustss 			mc.type = AUDIO_MIXER_SET;
   1150  1.11  augustss 			mc.un.mask = 0;
   1151  1.10  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
   1152   1.7  augustss 				if (idat & (1 << i)) {
   1153  1.43  augustss 					if (di->devmap[i] == -1) {
   1154  1.65  christos 						DPRINTF(("%s: OSS_SOUND_MIXER_"
   1155  1.65  christos 						    "WRITE_RECSRC bad devmap "
   1156  1.65  christos 						    "%d\n", __func__, i));
   1157  1.43  augustss 						error = EINVAL;
   1158  1.43  augustss 						goto out;
   1159  1.43  augustss 					}
   1160  1.65  christos 					mc.un.mask |= enum_to_mask(di,
   1161  1.65  christos 					    di->devmap[i]);
   1162   1.7  augustss 				}
   1163  1.10  augustss 			}
   1164   1.7  augustss 		}
   1165  1.62        ad 		error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
   1166  1.65  christos 		if (error) {
   1167  1.65  christos 			DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
   1168  1.65  christos 			    __func__, error));
   1169  1.65  christos 			goto out;
   1170  1.65  christos 		}
   1171  1.28   thorpej 		goto out;
   1172   1.6  augustss 	default:
   1173   1.6  augustss 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
   1174   1.6  augustss 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
   1175   1.6  augustss 			n = OSS_GET_DEV(com);
   1176  1.28   thorpej 			if (di->devmap[n] == -1) {
   1177  1.65  christos 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
   1178  1.65  christos 				    __func__, com, n));
   1179  1.28   thorpej 				error = EINVAL;
   1180  1.28   thorpej 				goto out;
   1181  1.28   thorpej 			}
   1182  1.17  augustss 		    doread:
   1183   1.6  augustss 			mc.dev = di->devmap[n];
   1184   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
   1185  1.65  christos 			mc.un.value.num_channels = di->stereomask &
   1186  1.65  christos 			    (1 << n) ? 2 : 1;
   1187  1.62        ad 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
   1188  1.65  christos 			if (error) {
   1189  1.65  christos 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
   1190  1.65  christos 				    __func__, error));
   1191  1.28   thorpej 				goto out;
   1192  1.65  christos 			}
   1193   1.6  augustss 			if (mc.un.value.num_channels != 2) {
   1194  1.65  christos 				l = r =
   1195  1.65  christos 				    mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
   1196   1.6  augustss 			} else {
   1197   1.6  augustss 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
   1198   1.6  augustss 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
   1199   1.6  augustss 			}
   1200  1.17  augustss 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
   1201  1.65  christos 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
   1202  1.65  christos 				 __func__, n, di->devmap[n], l, r, idat));
   1203   1.6  augustss 			break;
   1204  1.15  augustss 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
   1205  1.65  christos 		    com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
   1206  1.65  christos 		    (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
   1207  1.65  christos 		    com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
   1208   1.6  augustss 			n = OSS_GET_DEV(com);
   1209  1.28   thorpej 			if (di->devmap[n] == -1) {
   1210  1.65  christos 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
   1211  1.65  christos 				    __func__, com, n));
   1212  1.28   thorpej 				error = EINVAL;
   1213  1.28   thorpej 				goto out;
   1214  1.28   thorpej 			}
   1215   1.6  augustss 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1216  1.65  christos 			if (error) {
   1217  1.65  christos 				DPRINTF(("%s: 0x%lx error %d\n",
   1218  1.65  christos 				    __func__, com, error));
   1219  1.28   thorpej 				goto out;
   1220  1.65  christos 			}
   1221  1.17  augustss 			l = FROM_OSSVOL( idat       & 0xff);
   1222  1.17  augustss 			r = FROM_OSSVOL((idat >> 8) & 0xff);
   1223   1.6  augustss 			mc.dev = di->devmap[n];
   1224   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
   1225  1.65  christos 			if (di->stereomask & (1 << n)) {
   1226   1.6  augustss 				mc.un.value.num_channels = 2;
   1227   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
   1228   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
   1229   1.6  augustss 			} else {
   1230   1.6  augustss 				mc.un.value.num_channels = 1;
   1231  1.65  christos 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] =
   1232  1.65  christos 				    (l + r) / 2;
   1233   1.6  augustss 			}
   1234  1.65  christos 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
   1235  1.65  christos 			     __func__, n, di->devmap[n], l, r, idat));
   1236  1.62        ad 			error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
   1237  1.65  christos 			if (error) {
   1238  1.65  christos 				DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
   1239  1.65  christos 				    __func__, error));
   1240  1.28   thorpej 				goto out;
   1241  1.65  christos 			}
   1242  1.15  augustss 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
   1243  1.28   thorpej 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
   1244  1.28   thorpej 				error = 0;
   1245  1.28   thorpej 				goto out;
   1246  1.28   thorpej 			}
   1247   1.6  augustss 			goto doread;
   1248  1.15  augustss 		} else {
   1249  1.65  christos 			DPRINTF(("%s: Unknown mixer ioctl 0x%lx\n", __func__,
   1250  1.65  christos 			    com));
   1251  1.28   thorpej 			error = EINVAL;
   1252  1.28   thorpej 			goto out;
   1253  1.15  augustss 		}
   1254   1.6  augustss 	}
   1255  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
   1256  1.28   thorpej  out:
   1257  1.62        ad  	fd_putfile(SCARG(uap, fd));
   1258  1.28   thorpej 	return error;
   1259   1.6  augustss }
   1260   1.6  augustss 
   1261  1.26  augustss /* Sequencer emulation */
   1262   1.3   mycroft int
   1263  1.61       dsl oss_ioctl_sequencer(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
   1264  1.61       dsl {
   1265  1.61       dsl 	/* {
   1266   1.3   mycroft 		syscallarg(int) fd;
   1267   1.3   mycroft 		syscallarg(u_long) com;
   1268  1.55  christos 		syscallarg(void *) data;
   1269  1.61       dsl 	} */
   1270  1.62        ad 	file_t *fp;
   1271   1.3   mycroft 	u_long com;
   1272  1.26  augustss 	int idat, idat1;
   1273  1.26  augustss 	struct synth_info si;
   1274  1.26  augustss 	struct oss_synth_info osi;
   1275  1.26  augustss 	struct oss_seq_event_rec oser;
   1276   1.3   mycroft 	int error;
   1277  1.62        ad 	int (*ioctlf)(file_t *, u_long, void *);
   1278   1.3   mycroft 
   1279  1.62        ad 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
   1280   1.3   mycroft 		return (EBADF);
   1281   1.3   mycroft 
   1282  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
   1283  1.28   thorpej 		error = EBADF;
   1284  1.28   thorpej 		goto out;
   1285  1.28   thorpej 	}
   1286   1.3   mycroft 
   1287   1.3   mycroft 	com = SCARG(uap, com);
   1288  1.65  christos 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
   1289  1.26  augustss 
   1290   1.3   mycroft 	retval[0] = 0;
   1291   1.3   mycroft 
   1292  1.26  augustss 	ioctlf = fp->f_ops->fo_ioctl;
   1293  1.26  augustss 	switch (com) {
   1294  1.26  augustss 	case OSS_SEQ_RESET:
   1295  1.62        ad 		error = ioctlf(fp, SEQUENCER_RESET, &idat);
   1296  1.28   thorpej 		goto out;
   1297  1.26  augustss 	case OSS_SEQ_SYNC:
   1298  1.62        ad 		error = ioctlf(fp, SEQUENCER_SYNC, &idat);
   1299  1.28   thorpej 		goto out;
   1300  1.26  augustss 	case OSS_SYNTH_INFO:
   1301  1.26  augustss 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
   1302  1.26  augustss 		if (error)
   1303  1.28   thorpej 			goto out;
   1304  1.26  augustss 		si.device = osi.device;
   1305  1.62        ad 		error = ioctlf(fp, SEQUENCER_INFO, &si);
   1306  1.26  augustss 		if (error)
   1307  1.28   thorpej 			goto out;
   1308  1.26  augustss 		strncpy(osi.name, si.name, sizeof osi.name);
   1309  1.26  augustss 		osi.device = si.device;
   1310  1.26  augustss 		switch(si.synth_type) {
   1311  1.47     perry 		case SYNTH_TYPE_FM:
   1312  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
   1313  1.47     perry 		case SYNTH_TYPE_SAMPLE:
   1314  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
   1315  1.47     perry 		case SYNTH_TYPE_MIDI:
   1316  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
   1317  1.26  augustss 		default:
   1318  1.26  augustss 			osi.synth_type = 0; break;
   1319  1.26  augustss 		}
   1320  1.26  augustss 		switch(si.synth_subtype) {
   1321  1.47     perry 		case SYNTH_SUB_FM_TYPE_ADLIB:
   1322  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
   1323  1.47     perry 		case SYNTH_SUB_FM_TYPE_OPL3:
   1324  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
   1325  1.47     perry 		case SYNTH_SUB_MIDI_TYPE_MPU401:
   1326  1.26  augustss 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
   1327  1.47     perry 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
   1328  1.26  augustss 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
   1329  1.26  augustss 		default:
   1330  1.26  augustss 			osi.synth_subtype = 0; break;
   1331  1.26  augustss 		}
   1332  1.26  augustss 		osi.perc_mode = 0;
   1333  1.26  augustss 		osi.nr_voices = si.nr_voices;
   1334  1.26  augustss 		osi.nr_drums = 0;
   1335  1.26  augustss 		osi.instr_bank_size = si.instr_bank_size;
   1336  1.26  augustss 		osi.capabilities = 0;
   1337  1.47     perry 		if (si.capabilities & SYNTH_CAP_OPL3)
   1338  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
   1339  1.26  augustss 		if (si.capabilities & SYNTH_CAP_INPUT)
   1340  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
   1341  1.28   thorpej 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
   1342  1.28   thorpej 		goto out;
   1343  1.26  augustss 	case OSS_SEQ_CTRLRATE:
   1344  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1345  1.26  augustss 		if (error)
   1346  1.28   thorpej 			goto out;
   1347  1.62        ad 		error = ioctlf(fp, SEQUENCER_CTRLRATE, &idat);
   1348  1.26  augustss 		if (error)
   1349  1.28   thorpej 			goto out;
   1350  1.26  augustss 		retval[0] = idat;
   1351  1.26  augustss 		break;
   1352  1.26  augustss 	case OSS_SEQ_GETOUTCOUNT:
   1353  1.62        ad 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, &idat);
   1354  1.26  augustss 		if (error)
   1355  1.28   thorpej 			goto out;
   1356  1.26  augustss 		retval[0] = idat;
   1357  1.26  augustss 		break;
   1358  1.26  augustss 	case OSS_SEQ_GETINCOUNT:
   1359  1.62        ad 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, &idat);
   1360  1.26  augustss 		if (error)
   1361  1.28   thorpej 			goto out;
   1362  1.26  augustss 		retval[0] = idat;
   1363  1.26  augustss 		break;
   1364  1.26  augustss 	case OSS_SEQ_NRSYNTHS:
   1365  1.62        ad 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, &idat);
   1366  1.26  augustss 		if (error)
   1367  1.28   thorpej 			goto out;
   1368  1.26  augustss 		retval[0] = idat;
   1369  1.26  augustss 		break;
   1370  1.26  augustss 	case OSS_SEQ_NRMIDIS:
   1371  1.62        ad 		error = ioctlf(fp, SEQUENCER_NRMIDIS, &idat);
   1372  1.26  augustss 		if (error)
   1373  1.28   thorpej 			goto out;
   1374  1.26  augustss 		retval[0] = idat;
   1375  1.26  augustss 		break;
   1376  1.26  augustss 	case OSS_SEQ_THRESHOLD:
   1377  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1378  1.26  augustss 		if (error)
   1379  1.28   thorpej 			goto out;
   1380  1.62        ad 		error = ioctlf(fp, SEQUENCER_THRESHOLD, &idat);
   1381  1.28   thorpej 		goto out;
   1382  1.26  augustss 	case OSS_MEMAVL:
   1383  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1384  1.26  augustss 		if (error)
   1385  1.28   thorpej 			goto out;
   1386  1.62        ad 		error = ioctlf(fp, SEQUENCER_MEMAVL, &idat);
   1387  1.26  augustss 		if (error)
   1388  1.28   thorpej 			goto out;
   1389  1.26  augustss 		retval[0] = idat;
   1390  1.26  augustss 		break;
   1391  1.26  augustss 	case OSS_SEQ_PANIC:
   1392  1.62        ad 		error = ioctlf(fp, SEQUENCER_PANIC, &idat);
   1393  1.28   thorpej 		goto out;
   1394  1.26  augustss 	case OSS_SEQ_OUTOFBAND:
   1395  1.26  augustss 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
   1396  1.26  augustss 		if (error)
   1397  1.28   thorpej 			goto out;
   1398  1.62        ad 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, &oser);
   1399  1.26  augustss 		if (error)
   1400  1.28   thorpej 			goto out;
   1401  1.26  augustss 		break;
   1402  1.26  augustss 	case OSS_SEQ_GETTIME:
   1403  1.62        ad 		error = ioctlf(fp, SEQUENCER_GETTIME, &idat);
   1404  1.26  augustss 		if (error)
   1405  1.28   thorpej 			goto out;
   1406  1.26  augustss 		retval[0] = idat;
   1407  1.26  augustss 		break;
   1408  1.26  augustss 	case OSS_TMR_TIMEBASE:
   1409  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1410  1.26  augustss 		if (error)
   1411  1.28   thorpej 			goto out;
   1412  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, &idat);
   1413  1.26  augustss 		if (error)
   1414  1.28   thorpej 			goto out;
   1415  1.26  augustss 		retval[0] = idat;
   1416  1.26  augustss 		break;
   1417  1.26  augustss 	case OSS_TMR_START:
   1418  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_START, &idat);
   1419  1.28   thorpej 		goto out;
   1420  1.26  augustss 	case OSS_TMR_STOP:
   1421  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_STOP, &idat);
   1422  1.28   thorpej 		goto out;
   1423  1.26  augustss 	case OSS_TMR_CONTINUE:
   1424  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, &idat);
   1425  1.28   thorpej 		goto out;
   1426  1.26  augustss 	case OSS_TMR_TEMPO:
   1427  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1428  1.26  augustss 		if (error)
   1429  1.28   thorpej 			goto out;
   1430  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, &idat);
   1431  1.26  augustss 		if (error)
   1432  1.28   thorpej 			goto out;
   1433  1.26  augustss 		retval[0] = idat;
   1434  1.26  augustss 		break;
   1435  1.26  augustss 	case OSS_TMR_SOURCE:
   1436  1.26  augustss 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
   1437  1.26  augustss 		if (error)
   1438  1.28   thorpej 			goto out;
   1439  1.26  augustss 		idat = 0;
   1440  1.26  augustss 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
   1441  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, &idat);
   1442  1.26  augustss 		if (error)
   1443  1.28   thorpej 			goto out;
   1444  1.26  augustss 		idat1 = idat;
   1445  1.26  augustss 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
   1446  1.26  augustss 		retval[0] = idat;
   1447  1.26  augustss 		break;
   1448  1.26  augustss 	case OSS_TMR_METRONOME:
   1449  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1450  1.26  augustss 		if (error)
   1451  1.28   thorpej 			goto out;
   1452  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, &idat);
   1453  1.28   thorpej 		goto out;
   1454  1.26  augustss 	case OSS_TMR_SELECT:
   1455  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1456  1.26  augustss 		if (error)
   1457  1.28   thorpej 			goto out;
   1458  1.26  augustss 		retval[0] = idat;
   1459  1.62        ad 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, &idat);
   1460  1.28   thorpej 		goto out;
   1461  1.26  augustss 	default:
   1462  1.65  christos 		DPRINTF(("%s: Unknown sequencer command 0x%lx\n", __func__,
   1463  1.65  christos 		    com));
   1464  1.28   thorpej 		error = EINVAL;
   1465  1.28   thorpej 		goto out;
   1466  1.26  augustss 	}
   1467  1.26  augustss 
   1468  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
   1469  1.28   thorpej  out:
   1470  1.62        ad 	fd_putfile(SCARG(uap, fd));
   1471  1.28   thorpej 	return error;
   1472  1.14  augustss }
   1473  1.14  augustss 
   1474  1.14  augustss /*
   1475  1.14  augustss  * Check that the blocksize is a power of 2 as OSS wants.
   1476  1.14  augustss  * If not, set it to be.
   1477  1.14  augustss  */
   1478  1.33  jdolecek static void
   1479  1.62        ad setblocksize(file_t *fp, struct audio_info *info)
   1480  1.14  augustss {
   1481  1.14  augustss 	struct audio_info set;
   1482  1.14  augustss 	int s;
   1483  1.14  augustss 
   1484  1.49  christos 	 if (info->blocksize & (info->blocksize-1)) {
   1485  1.14  augustss 		for(s = 32; s < info->blocksize; s <<= 1)
   1486  1.14  augustss 			;
   1487  1.50   xtraeme 		AUDIO_INITINFO(&set);
   1488  1.14  augustss 		set.blocksize = s;
   1489  1.62        ad 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, &set);
   1490  1.62        ad 		fp->f_ops->fo_ioctl(fp, AUDIO_GETBUFINFO, info);
   1491  1.14  augustss 	}
   1492   1.1   mycroft }
   1493