Home | History | Annotate | Line # | Download | only in ossaudio
ossaudio.c revision 1.28
      1  1.28   thorpej /*	$NetBSD: ossaudio.c,v 1.28 1999/05/05 20:01:05 thorpej Exp $	*/
      2  1.22  augustss 
      3  1.27  augustss /*-
      4  1.22  augustss  * Copyright (c) 1997 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  * 3. All advertising materials mentioning features or use of this software
     16  1.22  augustss  *    must display the following acknowledgement:
     17  1.22  augustss  *        This product includes software developed by the NetBSD
     18  1.22  augustss  *        Foundation, Inc. and its contributors.
     19  1.22  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.22  augustss  *    contributors may be used to endorse or promote products derived
     21  1.22  augustss  *    from this software without specific prior written permission.
     22  1.22  augustss  *
     23  1.22  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.22  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.22  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.22  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.22  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.22  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.22  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.22  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.22  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.22  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.22  augustss  * POSSIBILITY OF SUCH DAMAGE.
     34  1.22  augustss  */
     35  1.22  augustss 
     36   1.1   mycroft #include <sys/param.h>
     37   1.1   mycroft #include <sys/proc.h>
     38   1.1   mycroft #include <sys/systm.h>
     39   1.1   mycroft #include <sys/file.h>
     40   1.6  augustss #include <sys/vnode.h>
     41   1.1   mycroft #include <sys/filedesc.h>
     42   1.1   mycroft #include <sys/ioctl.h>
     43   1.1   mycroft #include <sys/mount.h>
     44  1.26  augustss #include <sys/kernel.h>
     45   1.1   mycroft #include <sys/audioio.h>
     46  1.26  augustss #include <sys/midiio.h>
     47   1.1   mycroft 
     48   1.1   mycroft #include <sys/syscallargs.h>
     49   1.1   mycroft 
     50   1.6  augustss #include <compat/ossaudio/ossaudio.h>
     51   1.6  augustss #include <compat/ossaudio/ossaudiovar.h>
     52   1.6  augustss 
     53  1.16  augustss #ifdef AUDIO_DEBUG
     54  1.16  augustss #define DPRINTF(x) if (ossdebug) printf x
     55  1.16  augustss int ossdebug = 0;
     56  1.16  augustss #else
     57  1.16  augustss #define DPRINTF(x)
     58  1.16  augustss #endif
     59  1.16  augustss 
     60  1.17  augustss #define TO_OSSVOL(x) ((x) * 100 / 255)
     61  1.17  augustss #define FROM_OSSVOL(x) ((x) * 255 / 100)
     62  1.17  augustss 
     63   1.6  augustss static struct audiodevinfo *getdevinfo __P((struct file *, struct proc *));
     64   1.1   mycroft 
     65  1.14  augustss static void setblocksize __P((struct file *, struct audio_info *, struct proc *));
     66  1.14  augustss 
     67  1.16  augustss 
     68   1.1   mycroft int
     69   1.6  augustss oss_ioctl_audio(p, uap, retval)
     70  1.21  augustss 	struct proc *p;
     71  1.21  augustss 	struct oss_sys_ioctl_args /* {
     72   1.1   mycroft 		syscallarg(int) fd;
     73   1.1   mycroft 		syscallarg(u_long) com;
     74   1.1   mycroft 		syscallarg(caddr_t) data;
     75   1.1   mycroft 	} */ *uap;
     76   1.1   mycroft 	register_t *retval;
     77   1.1   mycroft {
     78  1.21  augustss 	struct file *fp;
     79  1.21  augustss 	struct filedesc *fdp;
     80   1.1   mycroft 	u_long com;
     81   1.1   mycroft 	struct audio_info tmpinfo;
     82  1.13  augustss 	struct audio_offset tmpoffs;
     83  1.13  augustss 	struct oss_audio_buf_info bufinfo;
     84  1.13  augustss 	struct oss_count_info cntinfo;
     85  1.16  augustss 	struct audio_encoding tmpenc;
     86  1.21  augustss 	u_int u;
     87  1.13  augustss 	int idat, idata;
     88  1.28   thorpej 	int error = 0;
     89   1.6  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
     90   1.1   mycroft 
     91   1.1   mycroft 	fdp = p->p_fd;
     92   1.1   mycroft 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
     93  1.28   thorpej 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
     94  1.28   thorpej 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
     95   1.1   mycroft 		return (EBADF);
     96   1.1   mycroft 
     97  1.28   thorpej 	FILE_USE(fp);
     98  1.28   thorpej 
     99  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    100  1.28   thorpej 		error = EBADF;
    101  1.28   thorpej 		goto out;
    102  1.28   thorpej 	}
    103   1.1   mycroft 
    104  1.26  augustss 	com = SCARG(uap, com);
    105  1.26  augustss 	DPRINTF(("oss_ioctl_audio: com=%08lx\n", com));
    106   1.6  augustss 
    107   1.1   mycroft 	retval[0] = 0;
    108   1.1   mycroft 
    109  1.26  augustss 	ioctlf = fp->f_ops->fo_ioctl;
    110   1.1   mycroft 	switch (com) {
    111   1.6  augustss 	case OSS_SNDCTL_DSP_RESET:
    112   1.6  augustss 		error = ioctlf(fp, AUDIO_FLUSH, (caddr_t)0, p);
    113   1.1   mycroft 		if (error)
    114  1.28   thorpej 			goto out;
    115   1.1   mycroft 		break;
    116   1.6  augustss 	case OSS_SNDCTL_DSP_SYNC:
    117   1.6  augustss 	case OSS_SNDCTL_DSP_POST:
    118   1.6  augustss 		error = ioctlf(fp, AUDIO_DRAIN, (caddr_t)0, p);
    119   1.1   mycroft 		if (error)
    120  1.28   thorpej 			goto out;
    121   1.1   mycroft 		break;
    122   1.6  augustss 	case OSS_SNDCTL_DSP_SPEED:
    123   1.1   mycroft 		AUDIO_INITINFO(&tmpinfo);
    124   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    125   1.1   mycroft 		if (error)
    126  1.28   thorpej 			goto out;
    127   1.1   mycroft 		tmpinfo.play.sample_rate =
    128   1.1   mycroft 		tmpinfo.record.sample_rate = idat;
    129  1.16  augustss 		error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    130  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_SPEED %d = %d\n",
    131  1.16  augustss 			 idat, error));
    132  1.16  augustss 		if (error)
    133  1.28   thorpej 			goto out;
    134   1.6  augustss 		/* fall into ... */
    135   1.6  augustss 	case OSS_SOUND_PCM_READ_RATE:
    136   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    137   1.1   mycroft 		if (error)
    138  1.28   thorpej 			goto out;
    139   1.1   mycroft 		idat = tmpinfo.play.sample_rate;
    140   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    141   1.1   mycroft 		if (error)
    142  1.28   thorpej 			goto out;
    143   1.1   mycroft 		break;
    144   1.6  augustss 	case OSS_SNDCTL_DSP_STEREO:
    145   1.1   mycroft 		AUDIO_INITINFO(&tmpinfo);
    146   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    147   1.1   mycroft 		if (error)
    148  1.28   thorpej 			goto out;
    149   1.1   mycroft 		tmpinfo.play.channels =
    150   1.1   mycroft 		tmpinfo.record.channels = idat ? 2 : 1;
    151   1.6  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    152   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    153   1.1   mycroft 		if (error)
    154  1.28   thorpej 			goto out;
    155   1.1   mycroft 		idat = tmpinfo.play.channels - 1;
    156   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    157   1.1   mycroft 		if (error)
    158  1.28   thorpej 			goto out;
    159   1.1   mycroft 		break;
    160   1.6  augustss 	case OSS_SNDCTL_DSP_GETBLKSIZE:
    161   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    162   1.1   mycroft 		if (error)
    163  1.28   thorpej 			goto out;
    164  1.14  augustss 		setblocksize(fp, &tmpinfo, p);
    165   1.1   mycroft 		idat = tmpinfo.blocksize;
    166   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    167   1.1   mycroft 		if (error)
    168  1.28   thorpej 			goto out;
    169   1.1   mycroft 		break;
    170   1.6  augustss 	case OSS_SNDCTL_DSP_SETFMT:
    171   1.1   mycroft 		AUDIO_INITINFO(&tmpinfo);
    172   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    173   1.1   mycroft 		if (error)
    174  1.28   thorpej 			goto out;
    175   1.1   mycroft 		switch (idat) {
    176   1.6  augustss 		case OSS_AFMT_MU_LAW:
    177   1.1   mycroft 			tmpinfo.play.precision =
    178   1.1   mycroft 			tmpinfo.record.precision = 8;
    179   1.1   mycroft 			tmpinfo.play.encoding =
    180   1.1   mycroft 			tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
    181   1.1   mycroft 			break;
    182   1.6  augustss 		case OSS_AFMT_A_LAW:
    183   1.1   mycroft 			tmpinfo.play.precision =
    184   1.1   mycroft 			tmpinfo.record.precision = 8;
    185   1.1   mycroft 			tmpinfo.play.encoding =
    186   1.1   mycroft 			tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
    187   1.1   mycroft 			break;
    188   1.6  augustss 		case OSS_AFMT_U8:
    189   1.1   mycroft 			tmpinfo.play.precision =
    190   1.1   mycroft 			tmpinfo.record.precision = 8;
    191   1.1   mycroft 			tmpinfo.play.encoding =
    192   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
    193   1.8  augustss 			break;
    194   1.8  augustss 		case OSS_AFMT_S8:
    195   1.8  augustss 			tmpinfo.play.precision =
    196   1.8  augustss 			tmpinfo.record.precision = 8;
    197   1.8  augustss 			tmpinfo.play.encoding =
    198  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
    199   1.1   mycroft 			break;
    200   1.6  augustss 		case OSS_AFMT_S16_LE:
    201   1.1   mycroft 			tmpinfo.play.precision =
    202   1.1   mycroft 			tmpinfo.record.precision = 16;
    203   1.1   mycroft 			tmpinfo.play.encoding =
    204  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
    205   1.8  augustss 			break;
    206   1.8  augustss 		case OSS_AFMT_S16_BE:
    207   1.8  augustss 			tmpinfo.play.precision =
    208   1.8  augustss 			tmpinfo.record.precision = 16;
    209   1.8  augustss 			tmpinfo.play.encoding =
    210  1.12  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
    211   1.8  augustss 			break;
    212   1.8  augustss 		case OSS_AFMT_U16_LE:
    213   1.8  augustss 			tmpinfo.play.precision =
    214   1.8  augustss 			tmpinfo.record.precision = 16;
    215   1.8  augustss 			tmpinfo.play.encoding =
    216   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
    217   1.8  augustss 			break;
    218   1.8  augustss 		case OSS_AFMT_U16_BE:
    219   1.8  augustss 			tmpinfo.play.precision =
    220   1.8  augustss 			tmpinfo.record.precision = 16;
    221   1.8  augustss 			tmpinfo.play.encoding =
    222   1.8  augustss 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
    223   1.1   mycroft 			break;
    224   1.1   mycroft 		default:
    225  1.28   thorpej 			error = EINVAL;
    226  1.28   thorpej 			goto out;
    227   1.1   mycroft 		}
    228   1.6  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    229   1.6  augustss 		/* fall into ... */
    230   1.6  augustss 	case OSS_SOUND_PCM_READ_BITS:
    231   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    232   1.1   mycroft 		if (error)
    233  1.28   thorpej 			goto out;
    234   1.5   mycroft 		switch (tmpinfo.play.encoding) {
    235   1.5   mycroft 		case AUDIO_ENCODING_ULAW:
    236   1.6  augustss 			idat = OSS_AFMT_MU_LAW;
    237   1.5   mycroft 			break;
    238   1.5   mycroft 		case AUDIO_ENCODING_ALAW:
    239   1.6  augustss 			idat = OSS_AFMT_A_LAW;
    240   1.5   mycroft 			break;
    241  1.12  augustss 		case AUDIO_ENCODING_SLINEAR_LE:
    242   1.8  augustss 			if (tmpinfo.play.precision == 16)
    243   1.8  augustss 				idat = OSS_AFMT_S16_LE;
    244   1.8  augustss 			else
    245   1.8  augustss 				idat = OSS_AFMT_S8;
    246   1.8  augustss 			break;
    247  1.12  augustss 		case AUDIO_ENCODING_SLINEAR_BE:
    248   1.8  augustss 			if (tmpinfo.play.precision == 16)
    249   1.8  augustss 				idat = OSS_AFMT_S16_BE;
    250   1.8  augustss 			else
    251   1.8  augustss 				idat = OSS_AFMT_S8;
    252   1.8  augustss 			break;
    253   1.8  augustss 		case AUDIO_ENCODING_ULINEAR_LE:
    254   1.8  augustss 			if (tmpinfo.play.precision == 16)
    255   1.8  augustss 				idat = OSS_AFMT_U16_LE;
    256   1.8  augustss 			else
    257   1.8  augustss 				idat = OSS_AFMT_U8;
    258   1.8  augustss 			break;
    259   1.8  augustss 		case AUDIO_ENCODING_ULINEAR_BE:
    260   1.8  augustss 			if (tmpinfo.play.precision == 16)
    261   1.8  augustss 				idat = OSS_AFMT_U16_BE;
    262   1.8  augustss 			else
    263   1.8  augustss 				idat = OSS_AFMT_U8;
    264   1.5   mycroft 			break;
    265   1.5   mycroft 		case AUDIO_ENCODING_ADPCM:
    266   1.6  augustss 			idat = OSS_AFMT_IMA_ADPCM;
    267   1.5   mycroft 			break;
    268   1.5   mycroft 		}
    269   1.5   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    270   1.5   mycroft 		if (error)
    271  1.28   thorpej 			goto out;
    272   1.1   mycroft 		break;
    273   1.6  augustss 	case OSS_SNDCTL_DSP_CHANNELS:
    274   1.6  augustss 		AUDIO_INITINFO(&tmpinfo);
    275   1.6  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    276   1.6  augustss 		if (error)
    277  1.28   thorpej 			goto out;
    278   1.6  augustss 		tmpinfo.play.channels =
    279   1.6  augustss 		tmpinfo.record.channels = idat;
    280   1.6  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    281   1.6  augustss 		/* fall into ... */
    282   1.6  augustss 	case OSS_SOUND_PCM_READ_CHANNELS:
    283   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    284   1.6  augustss 		if (error)
    285  1.28   thorpej 			goto out;
    286   1.6  augustss 		idat = tmpinfo.play.channels;
    287   1.6  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    288   1.6  augustss 		if (error)
    289  1.28   thorpej 			goto out;
    290   1.6  augustss 		break;
    291   1.6  augustss 	case OSS_SOUND_PCM_WRITE_FILTER:
    292   1.6  augustss 	case OSS_SOUND_PCM_READ_FILTER:
    293  1.28   thorpej 		error = EINVAL; /* XXX unimplemented */
    294  1.28   thorpej 		goto out;
    295   1.6  augustss 	case OSS_SNDCTL_DSP_SUBDIVIDE:
    296   1.9  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    297   1.9  augustss 		if (error)
    298  1.28   thorpej 			goto out;
    299   1.9  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    300  1.14  augustss 		setblocksize(fp, &tmpinfo, p);
    301   1.9  augustss 		if (error)
    302  1.28   thorpej 			goto out;
    303   1.9  augustss 		if (idat == 0)
    304  1.23  augustss 			idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
    305  1.23  augustss 		idat = (tmpinfo.play.buffer_size / idat) & -4;
    306   1.9  augustss 		AUDIO_INITINFO(&tmpinfo);
    307   1.9  augustss 		tmpinfo.blocksize = idat;
    308   1.9  augustss 		error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    309   1.9  augustss 		if (error)
    310  1.28   thorpej 			goto out;
    311  1.23  augustss 		idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
    312   1.9  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    313   1.9  augustss 		if (error)
    314  1.28   thorpej 			goto out;
    315   1.9  augustss 		break;
    316   1.6  augustss 	case OSS_SNDCTL_DSP_SETFRAGMENT:
    317   1.1   mycroft 		AUDIO_INITINFO(&tmpinfo);
    318   1.1   mycroft 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    319   1.1   mycroft 		if (error)
    320  1.28   thorpej 			goto out;
    321  1.28   thorpej 		if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
    322  1.28   thorpej 			error = EINVAL;
    323  1.28   thorpej 			goto out;
    324  1.28   thorpej 		}
    325   1.1   mycroft 		tmpinfo.blocksize = 1 << (idat & 0xffff);
    326  1.24   mycroft 		tmpinfo.hiwat = (idat >> 16) & 0x7fff;
    327  1.21  augustss 		DPRINTF(("oss_audio: SETFRAGMENT blksize=%d, hiwat=%d\n",
    328  1.21  augustss 			 tmpinfo.blocksize, tmpinfo.hiwat));
    329  1.21  augustss 		if (tmpinfo.hiwat == 0)	/* 0 means set to max */
    330  1.21  augustss 			tmpinfo.hiwat = 65536;
    331   1.6  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    332   1.6  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    333   1.1   mycroft 		if (error)
    334  1.28   thorpej 			goto out;
    335  1.21  augustss 		u = tmpinfo.blocksize;
    336  1.25  augustss 		for(idat = 0; u > 1; idat++, u >>= 1)
    337  1.21  augustss 			;
    338  1.24   mycroft 		idat |= (tmpinfo.hiwat & 0x7fff) << 16;
    339   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    340   1.1   mycroft 		if (error)
    341  1.28   thorpej 			goto out;
    342   1.1   mycroft 		break;
    343   1.6  augustss 	case OSS_SNDCTL_DSP_GETFMTS:
    344  1.16  augustss 		for(idat = 0, tmpenc.index = 0;
    345  1.16  augustss 		    ioctlf(fp, AUDIO_GETENC, (caddr_t)&tmpenc, p) == 0;
    346  1.16  augustss 		    tmpenc.index++) {
    347  1.16  augustss 			if (tmpenc.flags & AUDIO_ENCODINGFLAG_EMULATED)
    348  1.16  augustss 				continue; /* Don't report emulated modes */
    349  1.16  augustss 			switch(tmpenc.encoding) {
    350  1.16  augustss 			case AUDIO_ENCODING_ULAW:
    351  1.16  augustss 				idat |= OSS_AFMT_MU_LAW;
    352  1.16  augustss 				break;
    353  1.16  augustss 			case AUDIO_ENCODING_ALAW:
    354  1.16  augustss 				idat |= OSS_AFMT_A_LAW;
    355  1.16  augustss 				break;
    356  1.16  augustss 			case AUDIO_ENCODING_SLINEAR:
    357  1.16  augustss 				idat |= OSS_AFMT_S8;
    358  1.16  augustss 				break;
    359  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_LE:
    360  1.16  augustss 				if (tmpenc.precision == 16)
    361  1.16  augustss 					idat |= OSS_AFMT_S16_LE;
    362  1.16  augustss 				else
    363  1.16  augustss 					idat |= OSS_AFMT_S8;
    364  1.16  augustss 				break;
    365  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_BE:
    366  1.16  augustss 				if (tmpenc.precision == 16)
    367  1.16  augustss 					idat |= OSS_AFMT_S16_BE;
    368  1.16  augustss 				else
    369  1.16  augustss 					idat |= OSS_AFMT_S8;
    370  1.16  augustss 				break;
    371  1.16  augustss 			case AUDIO_ENCODING_ULINEAR:
    372  1.16  augustss 				idat |= OSS_AFMT_U8;
    373  1.16  augustss 				break;
    374  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_LE:
    375  1.16  augustss 				if (tmpenc.precision == 16)
    376  1.16  augustss 					idat |= OSS_AFMT_U16_LE;
    377  1.16  augustss 				else
    378  1.16  augustss 					idat |= OSS_AFMT_U8;
    379  1.16  augustss 				break;
    380  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_BE:
    381  1.16  augustss 				if (tmpenc.precision == 16)
    382  1.16  augustss 					idat |= OSS_AFMT_U16_BE;
    383  1.16  augustss 				else
    384  1.16  augustss 					idat |= OSS_AFMT_U8;
    385  1.16  augustss 				break;
    386  1.16  augustss 			case AUDIO_ENCODING_ADPCM:
    387  1.16  augustss 				idat |= OSS_AFMT_IMA_ADPCM;
    388  1.16  augustss 				break;
    389  1.16  augustss 			default:
    390  1.16  augustss 				break;
    391  1.16  augustss 			}
    392  1.16  augustss 		}
    393  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETFMTS = %x\n", idat));
    394   1.6  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    395   1.6  augustss 		if (error)
    396  1.28   thorpej 			goto out;
    397   1.6  augustss 		break;
    398   1.6  augustss 	case OSS_SNDCTL_DSP_GETOSPACE:
    399  1.14  augustss 	case OSS_SNDCTL_DSP_GETISPACE:
    400  1.13  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    401  1.13  augustss 		if (error)
    402  1.28   thorpej 			goto out;
    403  1.14  augustss 		setblocksize(fp, &tmpinfo, p);
    404  1.13  augustss 		bufinfo.fragsize = tmpinfo.blocksize;
    405  1.13  augustss 		bufinfo.fragments = /* XXX */
    406  1.23  augustss 		bufinfo.fragstotal = tmpinfo.play.buffer_size / bufinfo.fragsize;
    407  1.23  augustss 		bufinfo.bytes = tmpinfo.play.buffer_size;
    408  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETxSPACE = %d %d %d %d\n",
    409  1.16  augustss 			 bufinfo.fragsize, bufinfo.fragments,
    410  1.16  augustss 			 bufinfo.fragstotal, bufinfo.bytes));
    411  1.13  augustss 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
    412  1.13  augustss 		if (error)
    413  1.28   thorpej 			goto out;
    414  1.13  augustss 		break;
    415   1.6  augustss 	case OSS_SNDCTL_DSP_NONBLOCK:
    416  1.18  augustss 		idat = 1;
    417  1.18  augustss 		error = ioctlf(fp, FIONBIO, (caddr_t)&idat, p);
    418  1.19  augustss 		if (error)
    419  1.28   thorpej 			goto out;
    420  1.18  augustss 		break;
    421   1.6  augustss 	case OSS_SNDCTL_DSP_GETCAPS:
    422  1.13  augustss 		error = ioctlf(fp, AUDIO_GETPROPS, (caddr_t)&idata, p);
    423  1.13  augustss 		if (error)
    424  1.28   thorpej 			goto out;
    425  1.13  augustss 		idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
    426  1.13  augustss 		if (idata & AUDIO_PROP_FULLDUPLEX)
    427  1.13  augustss 			idat |= OSS_DSP_CAP_DUPLEX;
    428  1.13  augustss 		if (idata & AUDIO_PROP_MMAP)
    429  1.13  augustss 			idat |= OSS_DSP_CAP_MMAP;
    430  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETCAPS = %x\n", idat));
    431  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    432  1.13  augustss 		if (error)
    433  1.28   thorpej 			goto out;
    434  1.13  augustss 		break;
    435  1.13  augustss #if 0
    436  1.13  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    437  1.13  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    438  1.13  augustss 		if (error)
    439  1.28   thorpej 			goto out;
    440  1.13  augustss 		idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
    441  1.13  augustss 		       (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
    442  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    443  1.13  augustss 		if (error)
    444  1.28   thorpej 			goto out;
    445  1.13  augustss 		break;
    446  1.13  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    447  1.13  augustss 		AUDIO_INITINFO(&tmpinfo);
    448  1.13  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    449  1.13  augustss 		if (error)
    450  1.28   thorpej 			goto out;
    451  1.13  augustss 		tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
    452  1.13  augustss 		tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
    453  1.13  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    454   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    455   1.1   mycroft 		if (error)
    456  1.28   thorpej 			goto out;
    457   1.1   mycroft 		break;
    458  1.13  augustss #else
    459   1.6  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    460   1.6  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    461  1.13  augustss 		/* XXX Do nothing for now. */
    462  1.13  augustss 		idat = OSS_PCM_ENABLE_OUTPUT;
    463  1.28   thorpej 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    464  1.28   thorpej 		goto out;
    465  1.13  augustss #endif
    466   1.6  augustss 	case OSS_SNDCTL_DSP_GETIPTR:
    467  1.13  augustss 		error = ioctlf(fp, AUDIO_GETIOFFS, (caddr_t)&tmpoffs, p);
    468  1.13  augustss 		if (error)
    469  1.28   thorpej 			goto out;
    470  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    471  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    472  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    473  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    474  1.13  augustss 		if (error)
    475  1.28   thorpej 			goto out;
    476  1.13  augustss 		break;
    477   1.6  augustss 	case OSS_SNDCTL_DSP_GETOPTR:
    478  1.13  augustss 		error = ioctlf(fp, AUDIO_GETOOFFS, (caddr_t)&tmpoffs, p);
    479  1.13  augustss 		if (error)
    480  1.28   thorpej 			goto out;
    481  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    482  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    483  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    484  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    485  1.13  augustss 		if (error)
    486  1.28   thorpej 			goto out;
    487  1.13  augustss 		break;
    488   1.6  augustss 	case OSS_SNDCTL_DSP_MAPINBUF:
    489   1.6  augustss 	case OSS_SNDCTL_DSP_MAPOUTBUF:
    490   1.6  augustss 	case OSS_SNDCTL_DSP_SETSYNCRO:
    491   1.6  augustss 	case OSS_SNDCTL_DSP_SETDUPLEX:
    492   1.6  augustss 	case OSS_SNDCTL_DSP_PROFILE:
    493  1.28   thorpej 		error = EINVAL;
    494  1.28   thorpej 		goto out;
    495   1.1   mycroft 	default:
    496  1.28   thorpej 		error = EINVAL;
    497  1.28   thorpej 		goto out;
    498   1.1   mycroft 	}
    499   1.1   mycroft 
    500  1.28   thorpej  out:
    501  1.28   thorpej 	FILE_UNUSE(fp, p);
    502  1.28   thorpej 	return error;
    503   1.3   mycroft }
    504   1.3   mycroft 
    505   1.6  augustss /* If the NetBSD mixer device should have more than 32 devices
    506   1.6  augustss  * some will not be available to Linux */
    507  1.20  augustss #define NETBSD_MAXDEVS 64
    508   1.6  augustss struct audiodevinfo {
    509   1.6  augustss 	int done;
    510   1.6  augustss 	dev_t dev;
    511   1.7  augustss 	int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
    512   1.7  augustss 	        rdevmap[NETBSD_MAXDEVS];
    513   1.6  augustss         u_long devmask, recmask, stereomask;
    514   1.6  augustss 	u_long caps, source;
    515   1.6  augustss };
    516   1.6  augustss 
    517   1.6  augustss /*
    518   1.6  augustss  * Collect the audio device information to allow faster
    519   1.6  augustss  * emulation of the Linux mixer ioctls.  Cache the information
    520   1.6  augustss  * to eliminate the overhead of repeating all the ioctls needed
    521   1.6  augustss  * to collect the information.
    522   1.6  augustss  */
    523   1.6  augustss static struct audiodevinfo *
    524   1.6  augustss getdevinfo(fp, p)
    525   1.6  augustss 	struct file *fp;
    526   1.6  augustss 	struct proc *p;
    527   1.6  augustss {
    528   1.6  augustss 	mixer_devinfo_t mi;
    529   1.6  augustss 	int i;
    530   1.6  augustss 	static struct {
    531   1.6  augustss 		char *name;
    532   1.6  augustss 		int code;
    533   1.6  augustss 	} *dp, devs[] = {
    534   1.6  augustss 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
    535   1.6  augustss 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
    536   1.6  augustss 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
    537   1.6  augustss 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
    538   1.6  augustss 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
    539  1.15  augustss 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
    540   1.6  augustss 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
    541   1.6  augustss 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
    542   1.6  augustss 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
    543   1.6  augustss /*		{ AudioNheadphone,	?? },*/
    544   1.6  augustss 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
    545   1.6  augustss 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
    546  1.15  augustss /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
    547   1.6  augustss /*		{ AudioNstereo,		?? },*/
    548   1.6  augustss /*		{ AudioNmono,		?? },*/
    549   1.6  augustss 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
    550   1.6  augustss /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
    551  1.10  augustss 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
    552   1.6  augustss /*		{ AudioNmixerout,	?? },*/
    553   1.6  augustss 		{ 0, -1 }
    554   1.6  augustss 	};
    555   1.6  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *)) =
    556   1.6  augustss 	    fp->f_ops->fo_ioctl;
    557   1.6  augustss 	struct vnode *vp;
    558   1.6  augustss 	struct vattr va;
    559   1.6  augustss 	static struct audiodevinfo devcache = { 0 };
    560   1.6  augustss 	struct audiodevinfo *di = &devcache;
    561   1.6  augustss 
    562   1.6  augustss 	/* Figure out what device it is so we can check if the
    563   1.6  augustss 	 * cached data is valid.
    564   1.6  augustss 	 */
    565   1.6  augustss 	vp = (struct vnode *)fp->f_data;
    566   1.6  augustss 	if (vp->v_type != VCHR)
    567   1.6  augustss 		return 0;
    568   1.6  augustss 	if (VOP_GETATTR(vp, &va, p->p_ucred, p))
    569   1.6  augustss 		return 0;
    570   1.6  augustss 	if (di->done && di->dev == va.va_rdev)
    571   1.6  augustss 		return di;
    572   1.6  augustss 
    573   1.6  augustss 	di->done = 1;
    574   1.6  augustss 	di->dev = va.va_rdev;
    575   1.6  augustss 	di->devmask = 0;
    576   1.6  augustss 	di->recmask = 0;
    577   1.6  augustss 	di->stereomask = 0;
    578   1.6  augustss 	di->source = -1;
    579   1.6  augustss 	di->caps = 0;
    580   1.6  augustss 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
    581   1.6  augustss 		di->devmap[i] = -1;
    582   1.6  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++)
    583   1.6  augustss 		di->rdevmap[i] = -1;
    584   1.6  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
    585   1.6  augustss 		mi.index = i;
    586   1.6  augustss 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0)
    587   1.6  augustss 			break;
    588   1.6  augustss 		switch(mi.type) {
    589   1.6  augustss 		case AUDIO_MIXER_VALUE:
    590   1.6  augustss 			for(dp = devs; dp->name; dp++)
    591   1.6  augustss 		    		if (strcmp(dp->name, mi.label.name) == 0)
    592   1.6  augustss 					break;
    593   1.6  augustss 			if (dp->code >= 0) {
    594   1.6  augustss 				di->devmap[dp->code] = i;
    595   1.6  augustss 				di->rdevmap[i] = dp->code;
    596   1.6  augustss 				di->devmask |= 1 << dp->code;
    597   1.6  augustss 				if (mi.un.v.num_channels == 2)
    598   1.6  augustss 					di->stereomask |= 1 << dp->code;
    599   1.6  augustss 			}
    600   1.6  augustss 			break;
    601   1.6  augustss 		case AUDIO_MIXER_ENUM:
    602   1.6  augustss 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    603   1.6  augustss 				int j;
    604   1.6  augustss 				di->source = i;
    605  1.10  augustss 				for(j = 0; j < mi.un.e.num_mem; j++)
    606   1.6  augustss 					di->recmask |= 1 << di->rdevmap[mi.un.e.member[j].ord];
    607   1.6  augustss 				di->caps = OSS_SOUND_CAP_EXCL_INPUT;
    608   1.6  augustss 			}
    609   1.6  augustss 			break;
    610   1.6  augustss 		case AUDIO_MIXER_SET:
    611   1.6  augustss 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    612   1.6  augustss 				int j;
    613   1.6  augustss 				di->source = i;
    614   1.6  augustss 				for(j = 0; j < mi.un.s.num_mem; j++) {
    615   1.6  augustss 					int k, mask = mi.un.s.member[j].mask;
    616   1.6  augustss 					if (mask) {
    617  1.10  augustss 						for(k = 0; !(mask & 1); mask >>= 1, k++)
    618   1.6  augustss 							;
    619   1.6  augustss 						di->recmask |= 1 << di->rdevmap[k];
    620   1.6  augustss 					}
    621   1.6  augustss 				}
    622   1.6  augustss 			}
    623   1.6  augustss 			break;
    624   1.6  augustss 		}
    625   1.6  augustss 	}
    626   1.6  augustss 	return di;
    627   1.6  augustss }
    628   1.6  augustss 
    629   1.6  augustss int
    630   1.6  augustss oss_ioctl_mixer(p, uap, retval)
    631  1.21  augustss 	struct proc *p;
    632  1.21  augustss 	struct oss_sys_ioctl_args /* {
    633   1.6  augustss 		syscallarg(int) fd;
    634   1.6  augustss 		syscallarg(u_long) com;
    635   1.6  augustss 		syscallarg(caddr_t) data;
    636   1.6  augustss 	} */ *uap;
    637   1.6  augustss 	register_t *retval;
    638   1.6  augustss {
    639   1.6  augustss 	struct file *fp;
    640   1.6  augustss 	struct filedesc *fdp;
    641   1.6  augustss 	u_long com;
    642   1.6  augustss 	struct audiodevinfo *di;
    643   1.6  augustss 	mixer_ctrl_t mc;
    644   1.6  augustss 	int idat;
    645   1.6  augustss 	int i;
    646   1.6  augustss 	int error;
    647   1.6  augustss 	int l, r, n;
    648   1.6  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
    649   1.6  augustss 
    650   1.6  augustss 	fdp = p->p_fd;
    651   1.6  augustss 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    652  1.28   thorpej 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    653  1.28   thorpej 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
    654   1.6  augustss 		return (EBADF);
    655   1.6  augustss 
    656  1.28   thorpej 	FILE_USE(fp);
    657  1.28   thorpej 
    658  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    659  1.28   thorpej 		error = EBADF;
    660  1.28   thorpej 		goto out;
    661  1.28   thorpej 	}
    662   1.6  augustss 
    663   1.6  augustss 	com = SCARG(uap, com);
    664  1.26  augustss 	DPRINTF(("oss_ioctl_mixer: com=%08lx\n", com));
    665  1.26  augustss 
    666   1.6  augustss 	retval[0] = 0;
    667   1.6  augustss 
    668   1.6  augustss 	di = getdevinfo(fp, p);
    669  1.28   thorpej 	if (di == 0) {
    670  1.28   thorpej 		error = EINVAL;
    671  1.28   thorpej 		goto out;
    672  1.28   thorpej 	}
    673   1.6  augustss 
    674   1.6  augustss 	ioctlf = fp->f_ops->fo_ioctl;
    675   1.6  augustss 	switch (com) {
    676   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECSRC:
    677  1.28   thorpej 		if (di->source == -1) {
    678  1.28   thorpej 			error = EINVAL;
    679  1.28   thorpej 			goto out;
    680  1.28   thorpej 		}
    681   1.6  augustss 		mc.dev = di->source;
    682   1.6  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
    683   1.6  augustss 			mc.type = AUDIO_MIXER_ENUM;
    684   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    685   1.6  augustss 			if (error)
    686  1.28   thorpej 				goto out;
    687   1.6  augustss 			idat = 1 << di->rdevmap[mc.un.ord];
    688   1.6  augustss 		} else {
    689   1.6  augustss 			int k;
    690   1.6  augustss 			unsigned int mask;
    691   1.6  augustss 			mc.type = AUDIO_MIXER_SET;
    692   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    693   1.6  augustss 			if (error)
    694  1.28   thorpej 				goto out;
    695   1.6  augustss 			idat = 0;
    696   1.6  augustss 			for(mask = mc.un.mask, k = 0; mask; mask >>= 1, k++)
    697  1.10  augustss 				if (mask & 1)
    698  1.10  augustss 					idat |= 1 << di->rdevmap[k];
    699   1.6  augustss 		}
    700   1.6  augustss 		break;
    701   1.6  augustss 	case OSS_SOUND_MIXER_READ_DEVMASK:
    702   1.6  augustss 		idat = di->devmask;
    703   1.6  augustss 		break;
    704   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECMASK:
    705   1.6  augustss 		idat = di->recmask;
    706   1.6  augustss 		break;
    707   1.6  augustss 	case OSS_SOUND_MIXER_READ_STEREODEVS:
    708   1.6  augustss 		idat = di->stereomask;
    709   1.6  augustss 		break;
    710   1.6  augustss 	case OSS_SOUND_MIXER_READ_CAPS:
    711   1.6  augustss 		idat = di->caps;
    712   1.6  augustss 		break;
    713   1.6  augustss 	case OSS_SOUND_MIXER_WRITE_RECSRC:
    714  1.15  augustss 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
    715  1.28   thorpej 		if (di->source == -1) {
    716  1.28   thorpej 			error = EINVAL;
    717  1.28   thorpej 			goto out;
    718  1.28   thorpej 		}
    719   1.6  augustss 		mc.dev = di->source;
    720   1.6  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    721   1.6  augustss 		if (error)
    722  1.28   thorpej 			goto out;
    723   1.7  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
    724   1.7  augustss 			mc.type = AUDIO_MIXER_ENUM;
    725   1.7  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
    726   1.7  augustss 				if (idat & (1 << i))
    727   1.7  augustss 					break;
    728   1.7  augustss 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
    729  1.28   thorpej 			    di->devmap[i] == -1) {
    730  1.28   thorpej 				error = EINVAL;
    731  1.28   thorpej 				goto out;
    732  1.28   thorpej 			}
    733   1.7  augustss 			mc.un.ord = di->devmap[i];
    734   1.7  augustss 		} else {
    735   1.7  augustss 			mc.type = AUDIO_MIXER_SET;
    736  1.11  augustss 			mc.un.mask = 0;
    737  1.10  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
    738   1.7  augustss 				if (idat & (1 << i)) {
    739  1.28   thorpej 					if (di->devmap[i] == -1) {
    740  1.28   thorpej 						error = EINVAL;
    741  1.28   thorpej 						goto out;
    742  1.28   thorpej 					}
    743   1.7  augustss 					mc.un.mask |= 1 << di->devmap[i];
    744   1.7  augustss 				}
    745  1.10  augustss 			}
    746   1.7  augustss 		}
    747  1.28   thorpej 		error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
    748  1.28   thorpej 		goto out;
    749   1.6  augustss 	default:
    750   1.6  augustss 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
    751   1.6  augustss 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
    752   1.6  augustss 			n = OSS_GET_DEV(com);
    753  1.28   thorpej 			if (di->devmap[n] == -1) {
    754  1.28   thorpej 				error = EINVAL;
    755  1.28   thorpej 				goto out;
    756  1.28   thorpej 			}
    757  1.17  augustss 		    doread:
    758   1.6  augustss 			mc.dev = di->devmap[n];
    759   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
    760   1.6  augustss 			mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
    761   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    762   1.6  augustss 			if (error)
    763  1.28   thorpej 				goto out;
    764   1.6  augustss 			if (mc.un.value.num_channels != 2) {
    765   1.6  augustss 				l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
    766   1.6  augustss 			} else {
    767   1.6  augustss 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    768   1.6  augustss 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    769   1.6  augustss 			}
    770  1.17  augustss 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
    771  1.17  augustss 			DPRINTF(("OSS_MIXER_READ  n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
    772  1.17  augustss 				 n, di->devmap[n], l, r, idat));
    773   1.6  augustss 			break;
    774  1.15  augustss 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
    775  1.15  augustss 			   com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
    776  1.15  augustss 			   (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
    777  1.15  augustss 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
    778   1.6  augustss 			n = OSS_GET_DEV(com);
    779  1.28   thorpej 			if (di->devmap[n] == -1) {
    780  1.28   thorpej 				error = EINVAL;
    781  1.28   thorpej 				goto out;
    782  1.28   thorpej 			}
    783   1.6  augustss 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
    784   1.6  augustss 			if (error)
    785  1.28   thorpej 				goto out;
    786  1.17  augustss 			l = FROM_OSSVOL( idat       & 0xff);
    787  1.17  augustss 			r = FROM_OSSVOL((idat >> 8) & 0xff);
    788   1.6  augustss 			mc.dev = di->devmap[n];
    789   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
    790   1.6  augustss 			if (di->stereomask & (1<<n)) {
    791   1.6  augustss 				mc.un.value.num_channels = 2;
    792   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    793   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    794   1.6  augustss 			} else {
    795   1.6  augustss 				mc.un.value.num_channels = 1;
    796   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
    797   1.6  augustss 			}
    798  1.17  augustss 			DPRINTF(("OSS_MIXER_WRITE n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
    799  1.17  augustss 				 n, di->devmap[n], l, r, idat));
    800   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
    801   1.6  augustss 			if (error)
    802  1.28   thorpej 				goto out;
    803  1.15  augustss 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
    804  1.28   thorpej 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
    805  1.28   thorpej 				error = 0;
    806  1.28   thorpej 				goto out;
    807  1.28   thorpej 			}
    808   1.6  augustss 			goto doread;
    809  1.15  augustss 		} else {
    810  1.15  augustss #ifdef AUDIO_DEBUG
    811  1.15  augustss 			printf("oss_audio: unknown mixer ioctl %04lx\n", com);
    812  1.15  augustss #endif
    813  1.28   thorpej 			error = EINVAL;
    814  1.28   thorpej 			goto out;
    815  1.15  augustss 		}
    816   1.6  augustss 	}
    817  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
    818  1.28   thorpej  out:
    819  1.28   thorpej 	FILE_UNUSE(fp, p);
    820  1.28   thorpej 	return error;
    821   1.6  augustss }
    822   1.6  augustss 
    823  1.26  augustss /* Sequencer emulation */
    824   1.3   mycroft int
    825   1.6  augustss oss_ioctl_sequencer(p, uap, retval)
    826  1.21  augustss 	struct proc *p;
    827  1.21  augustss 	struct oss_sys_ioctl_args /* {
    828   1.3   mycroft 		syscallarg(int) fd;
    829   1.3   mycroft 		syscallarg(u_long) com;
    830   1.3   mycroft 		syscallarg(caddr_t) data;
    831   1.3   mycroft 	} */ *uap;
    832   1.3   mycroft 	register_t *retval;
    833   1.3   mycroft {
    834  1.21  augustss 	struct file *fp;
    835  1.21  augustss 	struct filedesc *fdp;
    836   1.3   mycroft 	u_long com;
    837  1.26  augustss 	int idat, idat1;
    838  1.26  augustss 	struct synth_info si;
    839  1.26  augustss 	struct oss_synth_info osi;
    840  1.26  augustss 	struct oss_seq_event_rec oser;
    841   1.3   mycroft 	int error;
    842  1.26  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
    843   1.3   mycroft 
    844   1.3   mycroft 	fdp = p->p_fd;
    845   1.3   mycroft 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    846  1.28   thorpej 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    847  1.28   thorpej 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
    848   1.3   mycroft 		return (EBADF);
    849   1.3   mycroft 
    850  1.28   thorpej 	FILE_USE(fp);
    851  1.28   thorpej 
    852  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    853  1.28   thorpej 		error = EBADF;
    854  1.28   thorpej 		goto out;
    855  1.28   thorpej 	}
    856   1.3   mycroft 
    857   1.3   mycroft 	com = SCARG(uap, com);
    858  1.26  augustss 	DPRINTF(("oss_ioctl_sequencer: com=%08lx\n", com));
    859  1.26  augustss 
    860   1.3   mycroft 	retval[0] = 0;
    861   1.3   mycroft 
    862  1.26  augustss 	ioctlf = fp->f_ops->fo_ioctl;
    863  1.26  augustss 	switch (com) {
    864  1.26  augustss 	case OSS_SEQ_RESET:
    865  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_RESET, (caddr_t)&idat, p);
    866  1.28   thorpej 		goto out;
    867  1.26  augustss 	case OSS_SEQ_SYNC:
    868  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_SYNC, (caddr_t)&idat, p);
    869  1.28   thorpej 		goto out;
    870  1.26  augustss 	case OSS_SYNTH_INFO:
    871  1.26  augustss 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
    872  1.26  augustss 		if (error)
    873  1.28   thorpej 			goto out;
    874  1.26  augustss 		si.device = osi.device;
    875  1.26  augustss 		error = ioctlf(fp, SEQUENCER_INFO, (caddr_t)&si, p);
    876  1.26  augustss 		if (error)
    877  1.28   thorpej 			goto out;
    878  1.26  augustss 		strncpy(osi.name, si.name, sizeof osi.name);
    879  1.26  augustss 		osi.device = si.device;
    880  1.26  augustss 		switch(si.synth_type) {
    881  1.26  augustss 		case SYNTH_TYPE_FM:
    882  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
    883  1.26  augustss 		case SYNTH_TYPE_SAMPLE:
    884  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
    885  1.26  augustss 		case SYNTH_TYPE_MIDI:
    886  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
    887  1.26  augustss 		default:
    888  1.26  augustss 			osi.synth_type = 0; break;
    889  1.26  augustss 		}
    890  1.26  augustss 		switch(si.synth_subtype) {
    891  1.26  augustss 		case SYNTH_SUB_FM_TYPE_ADLIB:
    892  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
    893  1.26  augustss 		case SYNTH_SUB_FM_TYPE_OPL3:
    894  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
    895  1.26  augustss 		case SYNTH_SUB_MIDI_TYPE_MPU401:
    896  1.26  augustss 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
    897  1.26  augustss 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
    898  1.26  augustss 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
    899  1.26  augustss 		default:
    900  1.26  augustss 			osi.synth_subtype = 0; break;
    901  1.26  augustss 		}
    902  1.26  augustss 		osi.perc_mode = 0;
    903  1.26  augustss 		osi.nr_voices = si.nr_voices;
    904  1.26  augustss 		osi.nr_drums = 0;
    905  1.26  augustss 		osi.instr_bank_size = si.instr_bank_size;
    906  1.26  augustss 		osi.capabilities = 0;
    907  1.26  augustss 		if (si.capabilities & SYNTH_CAP_OPL3)
    908  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
    909  1.26  augustss 		if (si.capabilities & SYNTH_CAP_INPUT)
    910  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
    911  1.28   thorpej 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
    912  1.28   thorpej 		goto out;
    913  1.26  augustss 	case OSS_SEQ_CTRLRATE:
    914  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    915  1.26  augustss 		if (error)
    916  1.28   thorpej 			goto out;
    917  1.26  augustss 		error = ioctlf(fp, SEQUENCER_CTRLRATE, (caddr_t)&idat, p);
    918  1.26  augustss 		if (error)
    919  1.28   thorpej 			goto out;
    920  1.26  augustss 		retval[0] = idat;
    921  1.26  augustss 		break;
    922  1.26  augustss 	case OSS_SEQ_GETOUTCOUNT:
    923  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, (caddr_t)&idat, p);
    924  1.26  augustss 		if (error)
    925  1.28   thorpej 			goto out;
    926  1.26  augustss 		retval[0] = idat;
    927  1.26  augustss 		break;
    928  1.26  augustss 	case OSS_SEQ_GETINCOUNT:
    929  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, (caddr_t)&idat, p);
    930  1.26  augustss 		if (error)
    931  1.28   thorpej 			goto out;
    932  1.26  augustss 		retval[0] = idat;
    933  1.26  augustss 		break;
    934  1.26  augustss 	case OSS_SEQ_NRSYNTHS:
    935  1.26  augustss 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, (caddr_t)&idat, p);
    936  1.26  augustss 		if (error)
    937  1.28   thorpej 			goto out;
    938  1.26  augustss 		retval[0] = idat;
    939  1.26  augustss 		break;
    940  1.26  augustss 	case OSS_SEQ_NRMIDIS:
    941  1.26  augustss 		error = ioctlf(fp, SEQUENCER_NRMIDIS, (caddr_t)&idat, p);
    942  1.26  augustss 		if (error)
    943  1.28   thorpej 			goto out;
    944  1.26  augustss 		retval[0] = idat;
    945  1.26  augustss 		break;
    946  1.26  augustss 	case OSS_SEQ_THRESHOLD:
    947  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    948  1.26  augustss 		if (error)
    949  1.28   thorpej 			goto out;
    950  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_THRESHOLD, (caddr_t)&idat, p);
    951  1.28   thorpej 		goto out;
    952  1.26  augustss 	case OSS_MEMAVL:
    953  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    954  1.26  augustss 		if (error)
    955  1.28   thorpej 			goto out;
    956  1.26  augustss 		error = ioctlf(fp, SEQUENCER_MEMAVL, (caddr_t)&idat, p);
    957  1.26  augustss 		if (error)
    958  1.28   thorpej 			goto out;
    959  1.26  augustss 		retval[0] = idat;
    960  1.26  augustss 		break;
    961  1.26  augustss 	case OSS_SEQ_PANIC:
    962  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_PANIC, (caddr_t)&idat, p);
    963  1.28   thorpej 		goto out;
    964  1.26  augustss 	case OSS_SEQ_OUTOFBAND:
    965  1.26  augustss 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
    966  1.26  augustss 		if (error)
    967  1.28   thorpej 			goto out;
    968  1.26  augustss 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, (caddr_t)&oser, p);
    969  1.26  augustss 		if (error)
    970  1.28   thorpej 			goto out;
    971  1.26  augustss 		break;
    972  1.26  augustss 	case OSS_SEQ_GETTIME:
    973  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETTIME, (caddr_t)&idat, p);
    974  1.26  augustss 		if (error)
    975  1.28   thorpej 			goto out;
    976  1.26  augustss 		retval[0] = idat;
    977  1.26  augustss 		break;
    978  1.26  augustss 	case OSS_TMR_TIMEBASE:
    979  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    980  1.26  augustss 		if (error)
    981  1.28   thorpej 			goto out;
    982  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, (caddr_t)&idat, p);
    983  1.26  augustss 		if (error)
    984  1.28   thorpej 			goto out;
    985  1.26  augustss 		retval[0] = idat;
    986  1.26  augustss 		break;
    987  1.26  augustss 	case OSS_TMR_START:
    988  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_START, (caddr_t)&idat, p);
    989  1.28   thorpej 		goto out;
    990  1.26  augustss 	case OSS_TMR_STOP:
    991  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_STOP, (caddr_t)&idat, p);
    992  1.28   thorpej 		goto out;
    993  1.26  augustss 	case OSS_TMR_CONTINUE:
    994  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, (caddr_t)&idat, p);
    995  1.28   thorpej 		goto out;
    996  1.26  augustss 	case OSS_TMR_TEMPO:
    997  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    998  1.26  augustss 		if (error)
    999  1.28   thorpej 			goto out;
   1000  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, (caddr_t)&idat, p);
   1001  1.26  augustss 		if (error)
   1002  1.28   thorpej 			goto out;
   1003  1.26  augustss 		retval[0] = idat;
   1004  1.26  augustss 		break;
   1005  1.26  augustss 	case OSS_TMR_SOURCE:
   1006  1.26  augustss 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
   1007  1.26  augustss 		if (error)
   1008  1.28   thorpej 			goto out;
   1009  1.26  augustss 		idat = 0;
   1010  1.26  augustss 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
   1011  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, (caddr_t)&idat, p);
   1012  1.26  augustss 		if (error)
   1013  1.28   thorpej 			goto out;
   1014  1.26  augustss 		idat1 = idat;
   1015  1.26  augustss 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
   1016  1.26  augustss 		retval[0] = idat;
   1017  1.26  augustss 		break;
   1018  1.26  augustss 	case OSS_TMR_METRONOME:
   1019  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1020  1.26  augustss 		if (error)
   1021  1.28   thorpej 			goto out;
   1022  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, (caddr_t)&idat, p);
   1023  1.28   thorpej 		goto out;
   1024  1.26  augustss 	case OSS_TMR_SELECT:
   1025  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1026  1.26  augustss 		if (error)
   1027  1.28   thorpej 			goto out;
   1028  1.26  augustss 		retval[0] = idat;
   1029  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, (caddr_t)&idat, p);
   1030  1.28   thorpej 		goto out;
   1031  1.26  augustss 	default:
   1032  1.28   thorpej 		error = EINVAL;
   1033  1.28   thorpej 		goto out;
   1034  1.26  augustss 	}
   1035  1.26  augustss 
   1036  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
   1037  1.28   thorpej  out:
   1038  1.28   thorpej 	FILE_UNUSE(fp, p);
   1039  1.28   thorpej 	return error;
   1040  1.14  augustss }
   1041  1.14  augustss 
   1042  1.14  augustss /*
   1043  1.14  augustss  * Check that the blocksize is a power of 2 as OSS wants.
   1044  1.14  augustss  * If not, set it to be.
   1045  1.14  augustss  */
   1046  1.14  augustss static void setblocksize(fp, info, p)
   1047  1.14  augustss 	struct file *fp;
   1048  1.14  augustss 	struct audio_info *info;
   1049  1.14  augustss 	struct proc *p;
   1050  1.14  augustss {
   1051  1.14  augustss 	struct audio_info set;
   1052  1.14  augustss 	int s;
   1053  1.14  augustss 
   1054  1.14  augustss 	if (info->blocksize & (info->blocksize-1)) {
   1055  1.14  augustss 		for(s = 32; s < info->blocksize; s <<= 1)
   1056  1.14  augustss 			;
   1057  1.14  augustss 		AUDIO_INITINFO(&set);
   1058  1.14  augustss 		set.blocksize = s;
   1059  1.14  augustss 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, (caddr_t)&set, p);
   1060  1.14  augustss 		fp->f_ops->fo_ioctl(fp, AUDIO_GETINFO, (caddr_t)info, p);
   1061  1.14  augustss 	}
   1062   1.1   mycroft }
   1063