Home | History | Annotate | Line # | Download | only in ossaudio
ossaudio.c revision 1.30
      1  1.30  augustss /*	$NetBSD: ossaudio.c,v 1.30 1999/11/17 00:06:38 augustss 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 			switch(tmpenc.encoding) {
    348  1.16  augustss 			case AUDIO_ENCODING_ULAW:
    349  1.16  augustss 				idat |= OSS_AFMT_MU_LAW;
    350  1.16  augustss 				break;
    351  1.16  augustss 			case AUDIO_ENCODING_ALAW:
    352  1.16  augustss 				idat |= OSS_AFMT_A_LAW;
    353  1.16  augustss 				break;
    354  1.16  augustss 			case AUDIO_ENCODING_SLINEAR:
    355  1.16  augustss 				idat |= OSS_AFMT_S8;
    356  1.16  augustss 				break;
    357  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_LE:
    358  1.16  augustss 				if (tmpenc.precision == 16)
    359  1.16  augustss 					idat |= OSS_AFMT_S16_LE;
    360  1.16  augustss 				else
    361  1.16  augustss 					idat |= OSS_AFMT_S8;
    362  1.16  augustss 				break;
    363  1.16  augustss 			case AUDIO_ENCODING_SLINEAR_BE:
    364  1.16  augustss 				if (tmpenc.precision == 16)
    365  1.16  augustss 					idat |= OSS_AFMT_S16_BE;
    366  1.16  augustss 				else
    367  1.16  augustss 					idat |= OSS_AFMT_S8;
    368  1.16  augustss 				break;
    369  1.16  augustss 			case AUDIO_ENCODING_ULINEAR:
    370  1.16  augustss 				idat |= OSS_AFMT_U8;
    371  1.16  augustss 				break;
    372  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_LE:
    373  1.16  augustss 				if (tmpenc.precision == 16)
    374  1.16  augustss 					idat |= OSS_AFMT_U16_LE;
    375  1.16  augustss 				else
    376  1.16  augustss 					idat |= OSS_AFMT_U8;
    377  1.16  augustss 				break;
    378  1.16  augustss 			case AUDIO_ENCODING_ULINEAR_BE:
    379  1.16  augustss 				if (tmpenc.precision == 16)
    380  1.16  augustss 					idat |= OSS_AFMT_U16_BE;
    381  1.16  augustss 				else
    382  1.16  augustss 					idat |= OSS_AFMT_U8;
    383  1.16  augustss 				break;
    384  1.16  augustss 			case AUDIO_ENCODING_ADPCM:
    385  1.16  augustss 				idat |= OSS_AFMT_IMA_ADPCM;
    386  1.16  augustss 				break;
    387  1.16  augustss 			default:
    388  1.16  augustss 				break;
    389  1.16  augustss 			}
    390  1.16  augustss 		}
    391  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETFMTS = %x\n", idat));
    392   1.6  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    393   1.6  augustss 		if (error)
    394  1.28   thorpej 			goto out;
    395   1.6  augustss 		break;
    396   1.6  augustss 	case OSS_SNDCTL_DSP_GETOSPACE:
    397  1.14  augustss 	case OSS_SNDCTL_DSP_GETISPACE:
    398  1.13  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    399  1.13  augustss 		if (error)
    400  1.28   thorpej 			goto out;
    401  1.14  augustss 		setblocksize(fp, &tmpinfo, p);
    402  1.13  augustss 		bufinfo.fragsize = tmpinfo.blocksize;
    403  1.13  augustss 		bufinfo.fragments = /* XXX */
    404  1.23  augustss 		bufinfo.fragstotal = tmpinfo.play.buffer_size / bufinfo.fragsize;
    405  1.23  augustss 		bufinfo.bytes = tmpinfo.play.buffer_size;
    406  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETxSPACE = %d %d %d %d\n",
    407  1.16  augustss 			 bufinfo.fragsize, bufinfo.fragments,
    408  1.16  augustss 			 bufinfo.fragstotal, bufinfo.bytes));
    409  1.13  augustss 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
    410  1.13  augustss 		if (error)
    411  1.28   thorpej 			goto out;
    412  1.13  augustss 		break;
    413   1.6  augustss 	case OSS_SNDCTL_DSP_NONBLOCK:
    414  1.18  augustss 		idat = 1;
    415  1.18  augustss 		error = ioctlf(fp, FIONBIO, (caddr_t)&idat, p);
    416  1.19  augustss 		if (error)
    417  1.28   thorpej 			goto out;
    418  1.18  augustss 		break;
    419   1.6  augustss 	case OSS_SNDCTL_DSP_GETCAPS:
    420  1.13  augustss 		error = ioctlf(fp, AUDIO_GETPROPS, (caddr_t)&idata, p);
    421  1.13  augustss 		if (error)
    422  1.28   thorpej 			goto out;
    423  1.13  augustss 		idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
    424  1.13  augustss 		if (idata & AUDIO_PROP_FULLDUPLEX)
    425  1.13  augustss 			idat |= OSS_DSP_CAP_DUPLEX;
    426  1.13  augustss 		if (idata & AUDIO_PROP_MMAP)
    427  1.13  augustss 			idat |= OSS_DSP_CAP_MMAP;
    428  1.16  augustss 		DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETCAPS = %x\n", idat));
    429  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    430  1.13  augustss 		if (error)
    431  1.28   thorpej 			goto out;
    432  1.13  augustss 		break;
    433  1.13  augustss #if 0
    434  1.13  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    435  1.13  augustss 		error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
    436  1.13  augustss 		if (error)
    437  1.28   thorpej 			goto out;
    438  1.13  augustss 		idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
    439  1.13  augustss 		       (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
    440  1.13  augustss 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    441  1.13  augustss 		if (error)
    442  1.28   thorpej 			goto out;
    443  1.13  augustss 		break;
    444  1.13  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    445  1.13  augustss 		AUDIO_INITINFO(&tmpinfo);
    446  1.13  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    447  1.13  augustss 		if (error)
    448  1.28   thorpej 			goto out;
    449  1.13  augustss 		tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
    450  1.13  augustss 		tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
    451  1.13  augustss 		(void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
    452   1.1   mycroft 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    453   1.1   mycroft 		if (error)
    454  1.28   thorpej 			goto out;
    455   1.1   mycroft 		break;
    456  1.13  augustss #else
    457   1.6  augustss 	case OSS_SNDCTL_DSP_GETTRIGGER:
    458   1.6  augustss 	case OSS_SNDCTL_DSP_SETTRIGGER:
    459  1.13  augustss 		/* XXX Do nothing for now. */
    460  1.13  augustss 		idat = OSS_PCM_ENABLE_OUTPUT;
    461  1.28   thorpej 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
    462  1.28   thorpej 		goto out;
    463  1.13  augustss #endif
    464   1.6  augustss 	case OSS_SNDCTL_DSP_GETIPTR:
    465  1.13  augustss 		error = ioctlf(fp, AUDIO_GETIOFFS, (caddr_t)&tmpoffs, p);
    466  1.13  augustss 		if (error)
    467  1.28   thorpej 			goto out;
    468  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    469  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    470  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    471  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    472  1.13  augustss 		if (error)
    473  1.28   thorpej 			goto out;
    474  1.13  augustss 		break;
    475   1.6  augustss 	case OSS_SNDCTL_DSP_GETOPTR:
    476  1.13  augustss 		error = ioctlf(fp, AUDIO_GETOOFFS, (caddr_t)&tmpoffs, p);
    477  1.13  augustss 		if (error)
    478  1.28   thorpej 			goto out;
    479  1.13  augustss 		cntinfo.bytes = tmpoffs.samples;
    480  1.13  augustss 		cntinfo.blocks = tmpoffs.deltablks;
    481  1.13  augustss 		cntinfo.ptr = tmpoffs.offset;
    482  1.13  augustss 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
    483  1.13  augustss 		if (error)
    484  1.28   thorpej 			goto out;
    485  1.13  augustss 		break;
    486   1.6  augustss 	case OSS_SNDCTL_DSP_MAPINBUF:
    487   1.6  augustss 	case OSS_SNDCTL_DSP_MAPOUTBUF:
    488   1.6  augustss 	case OSS_SNDCTL_DSP_SETSYNCRO:
    489   1.6  augustss 	case OSS_SNDCTL_DSP_SETDUPLEX:
    490   1.6  augustss 	case OSS_SNDCTL_DSP_PROFILE:
    491  1.28   thorpej 		error = EINVAL;
    492  1.28   thorpej 		goto out;
    493   1.1   mycroft 	default:
    494  1.28   thorpej 		error = EINVAL;
    495  1.28   thorpej 		goto out;
    496   1.1   mycroft 	}
    497   1.1   mycroft 
    498  1.28   thorpej  out:
    499  1.28   thorpej 	FILE_UNUSE(fp, p);
    500  1.28   thorpej 	return error;
    501   1.3   mycroft }
    502   1.3   mycroft 
    503   1.6  augustss /* If the NetBSD mixer device should have more than 32 devices
    504   1.6  augustss  * some will not be available to Linux */
    505  1.20  augustss #define NETBSD_MAXDEVS 64
    506   1.6  augustss struct audiodevinfo {
    507   1.6  augustss 	int done;
    508   1.6  augustss 	dev_t dev;
    509   1.7  augustss 	int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
    510   1.7  augustss 	        rdevmap[NETBSD_MAXDEVS];
    511   1.6  augustss         u_long devmask, recmask, stereomask;
    512   1.6  augustss 	u_long caps, source;
    513   1.6  augustss };
    514   1.6  augustss 
    515   1.6  augustss /*
    516   1.6  augustss  * Collect the audio device information to allow faster
    517   1.6  augustss  * emulation of the Linux mixer ioctls.  Cache the information
    518   1.6  augustss  * to eliminate the overhead of repeating all the ioctls needed
    519   1.6  augustss  * to collect the information.
    520   1.6  augustss  */
    521   1.6  augustss static struct audiodevinfo *
    522   1.6  augustss getdevinfo(fp, p)
    523   1.6  augustss 	struct file *fp;
    524   1.6  augustss 	struct proc *p;
    525   1.6  augustss {
    526   1.6  augustss 	mixer_devinfo_t mi;
    527   1.6  augustss 	int i;
    528   1.6  augustss 	static struct {
    529   1.6  augustss 		char *name;
    530   1.6  augustss 		int code;
    531   1.6  augustss 	} *dp, devs[] = {
    532   1.6  augustss 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
    533   1.6  augustss 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
    534   1.6  augustss 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
    535   1.6  augustss 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
    536   1.6  augustss 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
    537  1.15  augustss 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
    538   1.6  augustss 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
    539   1.6  augustss 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
    540   1.6  augustss 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
    541   1.6  augustss /*		{ AudioNheadphone,	?? },*/
    542   1.6  augustss 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
    543   1.6  augustss 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
    544  1.15  augustss /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
    545   1.6  augustss /*		{ AudioNstereo,		?? },*/
    546   1.6  augustss /*		{ AudioNmono,		?? },*/
    547   1.6  augustss 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
    548   1.6  augustss /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
    549  1.10  augustss 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
    550   1.6  augustss /*		{ AudioNmixerout,	?? },*/
    551   1.6  augustss 		{ 0, -1 }
    552   1.6  augustss 	};
    553   1.6  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *)) =
    554   1.6  augustss 	    fp->f_ops->fo_ioctl;
    555   1.6  augustss 	struct vnode *vp;
    556   1.6  augustss 	struct vattr va;
    557   1.6  augustss 	static struct audiodevinfo devcache = { 0 };
    558   1.6  augustss 	struct audiodevinfo *di = &devcache;
    559   1.6  augustss 
    560   1.6  augustss 	/* Figure out what device it is so we can check if the
    561   1.6  augustss 	 * cached data is valid.
    562   1.6  augustss 	 */
    563   1.6  augustss 	vp = (struct vnode *)fp->f_data;
    564   1.6  augustss 	if (vp->v_type != VCHR)
    565   1.6  augustss 		return 0;
    566   1.6  augustss 	if (VOP_GETATTR(vp, &va, p->p_ucred, p))
    567   1.6  augustss 		return 0;
    568   1.6  augustss 	if (di->done && di->dev == va.va_rdev)
    569   1.6  augustss 		return di;
    570   1.6  augustss 
    571   1.6  augustss 	di->done = 1;
    572   1.6  augustss 	di->dev = va.va_rdev;
    573   1.6  augustss 	di->devmask = 0;
    574   1.6  augustss 	di->recmask = 0;
    575   1.6  augustss 	di->stereomask = 0;
    576   1.6  augustss 	di->source = -1;
    577   1.6  augustss 	di->caps = 0;
    578   1.6  augustss 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
    579   1.6  augustss 		di->devmap[i] = -1;
    580   1.6  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++)
    581   1.6  augustss 		di->rdevmap[i] = -1;
    582   1.6  augustss 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
    583   1.6  augustss 		mi.index = i;
    584   1.6  augustss 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0)
    585   1.6  augustss 			break;
    586   1.6  augustss 		switch(mi.type) {
    587   1.6  augustss 		case AUDIO_MIXER_VALUE:
    588   1.6  augustss 			for(dp = devs; dp->name; dp++)
    589   1.6  augustss 		    		if (strcmp(dp->name, mi.label.name) == 0)
    590   1.6  augustss 					break;
    591   1.6  augustss 			if (dp->code >= 0) {
    592   1.6  augustss 				di->devmap[dp->code] = i;
    593   1.6  augustss 				di->rdevmap[i] = dp->code;
    594   1.6  augustss 				di->devmask |= 1 << dp->code;
    595   1.6  augustss 				if (mi.un.v.num_channels == 2)
    596   1.6  augustss 					di->stereomask |= 1 << dp->code;
    597   1.6  augustss 			}
    598   1.6  augustss 			break;
    599   1.6  augustss 		case AUDIO_MIXER_ENUM:
    600   1.6  augustss 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    601   1.6  augustss 				int j;
    602   1.6  augustss 				di->source = i;
    603  1.10  augustss 				for(j = 0; j < mi.un.e.num_mem; j++)
    604   1.6  augustss 					di->recmask |= 1 << di->rdevmap[mi.un.e.member[j].ord];
    605   1.6  augustss 				di->caps = OSS_SOUND_CAP_EXCL_INPUT;
    606   1.6  augustss 			}
    607   1.6  augustss 			break;
    608   1.6  augustss 		case AUDIO_MIXER_SET:
    609   1.6  augustss 			if (strcmp(mi.label.name, AudioNsource) == 0) {
    610   1.6  augustss 				int j;
    611   1.6  augustss 				di->source = i;
    612   1.6  augustss 				for(j = 0; j < mi.un.s.num_mem; j++) {
    613   1.6  augustss 					int k, mask = mi.un.s.member[j].mask;
    614   1.6  augustss 					if (mask) {
    615  1.10  augustss 						for(k = 0; !(mask & 1); mask >>= 1, k++)
    616   1.6  augustss 							;
    617   1.6  augustss 						di->recmask |= 1 << di->rdevmap[k];
    618   1.6  augustss 					}
    619   1.6  augustss 				}
    620   1.6  augustss 			}
    621   1.6  augustss 			break;
    622   1.6  augustss 		}
    623   1.6  augustss 	}
    624   1.6  augustss 	return di;
    625   1.6  augustss }
    626   1.6  augustss 
    627   1.6  augustss int
    628   1.6  augustss oss_ioctl_mixer(p, uap, retval)
    629  1.21  augustss 	struct proc *p;
    630  1.21  augustss 	struct oss_sys_ioctl_args /* {
    631   1.6  augustss 		syscallarg(int) fd;
    632   1.6  augustss 		syscallarg(u_long) com;
    633   1.6  augustss 		syscallarg(caddr_t) data;
    634   1.6  augustss 	} */ *uap;
    635   1.6  augustss 	register_t *retval;
    636   1.6  augustss {
    637   1.6  augustss 	struct file *fp;
    638   1.6  augustss 	struct filedesc *fdp;
    639   1.6  augustss 	u_long com;
    640   1.6  augustss 	struct audiodevinfo *di;
    641   1.6  augustss 	mixer_ctrl_t mc;
    642  1.30  augustss 	struct oss_mixer_info omi;
    643  1.30  augustss 	struct audio_device adev;
    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.30  augustss 	case OSS_SOUND_MIXER_INFO:
    677  1.30  augustss 	case OSS_SOUND_OLD_MIXER_INFO:
    678  1.30  augustss 		error = ioctlf(fp, AUDIO_GETDEV, (caddr_t)&adev, p);
    679  1.30  augustss 		if (error)
    680  1.30  augustss 			return (error);
    681  1.30  augustss 		omi.modify_counter = 1;
    682  1.30  augustss 		strncpy(omi.id, adev.name, sizeof omi.id);
    683  1.30  augustss 		strncpy(omi.name, adev.name, sizeof omi.name);
    684  1.30  augustss 		return copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
    685   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECSRC:
    686  1.28   thorpej 		if (di->source == -1) {
    687  1.28   thorpej 			error = EINVAL;
    688  1.28   thorpej 			goto out;
    689  1.28   thorpej 		}
    690   1.6  augustss 		mc.dev = di->source;
    691   1.6  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
    692   1.6  augustss 			mc.type = AUDIO_MIXER_ENUM;
    693   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    694   1.6  augustss 			if (error)
    695  1.28   thorpej 				goto out;
    696   1.6  augustss 			idat = 1 << di->rdevmap[mc.un.ord];
    697   1.6  augustss 		} else {
    698   1.6  augustss 			int k;
    699   1.6  augustss 			unsigned int mask;
    700   1.6  augustss 			mc.type = AUDIO_MIXER_SET;
    701   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    702   1.6  augustss 			if (error)
    703  1.28   thorpej 				goto out;
    704   1.6  augustss 			idat = 0;
    705   1.6  augustss 			for(mask = mc.un.mask, k = 0; mask; mask >>= 1, k++)
    706  1.10  augustss 				if (mask & 1)
    707  1.10  augustss 					idat |= 1 << di->rdevmap[k];
    708   1.6  augustss 		}
    709   1.6  augustss 		break;
    710   1.6  augustss 	case OSS_SOUND_MIXER_READ_DEVMASK:
    711   1.6  augustss 		idat = di->devmask;
    712   1.6  augustss 		break;
    713   1.6  augustss 	case OSS_SOUND_MIXER_READ_RECMASK:
    714   1.6  augustss 		idat = di->recmask;
    715   1.6  augustss 		break;
    716   1.6  augustss 	case OSS_SOUND_MIXER_READ_STEREODEVS:
    717   1.6  augustss 		idat = di->stereomask;
    718   1.6  augustss 		break;
    719   1.6  augustss 	case OSS_SOUND_MIXER_READ_CAPS:
    720   1.6  augustss 		idat = di->caps;
    721   1.6  augustss 		break;
    722   1.6  augustss 	case OSS_SOUND_MIXER_WRITE_RECSRC:
    723  1.15  augustss 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
    724  1.28   thorpej 		if (di->source == -1) {
    725  1.28   thorpej 			error = EINVAL;
    726  1.28   thorpej 			goto out;
    727  1.28   thorpej 		}
    728   1.6  augustss 		mc.dev = di->source;
    729   1.6  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    730   1.6  augustss 		if (error)
    731  1.28   thorpej 			goto out;
    732   1.7  augustss 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
    733   1.7  augustss 			mc.type = AUDIO_MIXER_ENUM;
    734   1.7  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
    735   1.7  augustss 				if (idat & (1 << i))
    736   1.7  augustss 					break;
    737   1.7  augustss 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
    738  1.28   thorpej 			    di->devmap[i] == -1) {
    739  1.28   thorpej 				error = EINVAL;
    740  1.28   thorpej 				goto out;
    741  1.28   thorpej 			}
    742   1.7  augustss 			mc.un.ord = di->devmap[i];
    743   1.7  augustss 		} else {
    744   1.7  augustss 			mc.type = AUDIO_MIXER_SET;
    745  1.11  augustss 			mc.un.mask = 0;
    746  1.10  augustss 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
    747   1.7  augustss 				if (idat & (1 << i)) {
    748  1.28   thorpej 					if (di->devmap[i] == -1) {
    749  1.28   thorpej 						error = EINVAL;
    750  1.28   thorpej 						goto out;
    751  1.28   thorpej 					}
    752   1.7  augustss 					mc.un.mask |= 1 << di->devmap[i];
    753   1.7  augustss 				}
    754  1.10  augustss 			}
    755   1.7  augustss 		}
    756  1.28   thorpej 		error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
    757  1.28   thorpej 		goto out;
    758   1.6  augustss 	default:
    759   1.6  augustss 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
    760   1.6  augustss 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
    761   1.6  augustss 			n = OSS_GET_DEV(com);
    762  1.28   thorpej 			if (di->devmap[n] == -1) {
    763  1.28   thorpej 				error = EINVAL;
    764  1.28   thorpej 				goto out;
    765  1.28   thorpej 			}
    766  1.17  augustss 		    doread:
    767   1.6  augustss 			mc.dev = di->devmap[n];
    768   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
    769   1.6  augustss 			mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
    770   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
    771   1.6  augustss 			if (error)
    772  1.28   thorpej 				goto out;
    773   1.6  augustss 			if (mc.un.value.num_channels != 2) {
    774   1.6  augustss 				l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
    775   1.6  augustss 			} else {
    776   1.6  augustss 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
    777   1.6  augustss 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
    778   1.6  augustss 			}
    779  1.17  augustss 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
    780  1.17  augustss 			DPRINTF(("OSS_MIXER_READ  n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
    781  1.17  augustss 				 n, di->devmap[n], l, r, idat));
    782   1.6  augustss 			break;
    783  1.15  augustss 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
    784  1.15  augustss 			   com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
    785  1.15  augustss 			   (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
    786  1.15  augustss 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
    787   1.6  augustss 			n = OSS_GET_DEV(com);
    788  1.28   thorpej 			if (di->devmap[n] == -1) {
    789  1.28   thorpej 				error = EINVAL;
    790  1.28   thorpej 				goto out;
    791  1.28   thorpej 			}
    792   1.6  augustss 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
    793   1.6  augustss 			if (error)
    794  1.28   thorpej 				goto out;
    795  1.17  augustss 			l = FROM_OSSVOL( idat       & 0xff);
    796  1.17  augustss 			r = FROM_OSSVOL((idat >> 8) & 0xff);
    797   1.6  augustss 			mc.dev = di->devmap[n];
    798   1.6  augustss 			mc.type = AUDIO_MIXER_VALUE;
    799   1.6  augustss 			if (di->stereomask & (1<<n)) {
    800   1.6  augustss 				mc.un.value.num_channels = 2;
    801   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
    802   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
    803   1.6  augustss 			} else {
    804   1.6  augustss 				mc.un.value.num_channels = 1;
    805   1.6  augustss 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
    806   1.6  augustss 			}
    807  1.17  augustss 			DPRINTF(("OSS_MIXER_WRITE n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
    808  1.17  augustss 				 n, di->devmap[n], l, r, idat));
    809   1.6  augustss 			error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
    810   1.6  augustss 			if (error)
    811  1.28   thorpej 				goto out;
    812  1.15  augustss 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
    813  1.28   thorpej 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
    814  1.28   thorpej 				error = 0;
    815  1.28   thorpej 				goto out;
    816  1.28   thorpej 			}
    817   1.6  augustss 			goto doread;
    818  1.15  augustss 		} else {
    819  1.15  augustss #ifdef AUDIO_DEBUG
    820  1.15  augustss 			printf("oss_audio: unknown mixer ioctl %04lx\n", com);
    821  1.15  augustss #endif
    822  1.28   thorpej 			error = EINVAL;
    823  1.28   thorpej 			goto out;
    824  1.15  augustss 		}
    825   1.6  augustss 	}
    826  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
    827  1.28   thorpej  out:
    828  1.28   thorpej 	FILE_UNUSE(fp, p);
    829  1.28   thorpej 	return error;
    830   1.6  augustss }
    831   1.6  augustss 
    832  1.26  augustss /* Sequencer emulation */
    833   1.3   mycroft int
    834   1.6  augustss oss_ioctl_sequencer(p, uap, retval)
    835  1.21  augustss 	struct proc *p;
    836  1.21  augustss 	struct oss_sys_ioctl_args /* {
    837   1.3   mycroft 		syscallarg(int) fd;
    838   1.3   mycroft 		syscallarg(u_long) com;
    839   1.3   mycroft 		syscallarg(caddr_t) data;
    840   1.3   mycroft 	} */ *uap;
    841   1.3   mycroft 	register_t *retval;
    842   1.3   mycroft {
    843  1.21  augustss 	struct file *fp;
    844  1.21  augustss 	struct filedesc *fdp;
    845   1.3   mycroft 	u_long com;
    846  1.26  augustss 	int idat, idat1;
    847  1.26  augustss 	struct synth_info si;
    848  1.26  augustss 	struct oss_synth_info osi;
    849  1.26  augustss 	struct oss_seq_event_rec oser;
    850   1.3   mycroft 	int error;
    851  1.26  augustss 	int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
    852   1.3   mycroft 
    853   1.3   mycroft 	fdp = p->p_fd;
    854   1.3   mycroft 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    855  1.28   thorpej 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    856  1.28   thorpej 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
    857   1.3   mycroft 		return (EBADF);
    858   1.3   mycroft 
    859  1.28   thorpej 	FILE_USE(fp);
    860  1.28   thorpej 
    861  1.28   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    862  1.28   thorpej 		error = EBADF;
    863  1.28   thorpej 		goto out;
    864  1.28   thorpej 	}
    865   1.3   mycroft 
    866   1.3   mycroft 	com = SCARG(uap, com);
    867  1.26  augustss 	DPRINTF(("oss_ioctl_sequencer: com=%08lx\n", com));
    868  1.26  augustss 
    869   1.3   mycroft 	retval[0] = 0;
    870   1.3   mycroft 
    871  1.26  augustss 	ioctlf = fp->f_ops->fo_ioctl;
    872  1.26  augustss 	switch (com) {
    873  1.26  augustss 	case OSS_SEQ_RESET:
    874  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_RESET, (caddr_t)&idat, p);
    875  1.28   thorpej 		goto out;
    876  1.26  augustss 	case OSS_SEQ_SYNC:
    877  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_SYNC, (caddr_t)&idat, p);
    878  1.28   thorpej 		goto out;
    879  1.26  augustss 	case OSS_SYNTH_INFO:
    880  1.26  augustss 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
    881  1.26  augustss 		if (error)
    882  1.28   thorpej 			goto out;
    883  1.26  augustss 		si.device = osi.device;
    884  1.26  augustss 		error = ioctlf(fp, SEQUENCER_INFO, (caddr_t)&si, p);
    885  1.26  augustss 		if (error)
    886  1.28   thorpej 			goto out;
    887  1.26  augustss 		strncpy(osi.name, si.name, sizeof osi.name);
    888  1.26  augustss 		osi.device = si.device;
    889  1.26  augustss 		switch(si.synth_type) {
    890  1.26  augustss 		case SYNTH_TYPE_FM:
    891  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
    892  1.26  augustss 		case SYNTH_TYPE_SAMPLE:
    893  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
    894  1.26  augustss 		case SYNTH_TYPE_MIDI:
    895  1.26  augustss 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
    896  1.26  augustss 		default:
    897  1.26  augustss 			osi.synth_type = 0; break;
    898  1.26  augustss 		}
    899  1.26  augustss 		switch(si.synth_subtype) {
    900  1.26  augustss 		case SYNTH_SUB_FM_TYPE_ADLIB:
    901  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
    902  1.26  augustss 		case SYNTH_SUB_FM_TYPE_OPL3:
    903  1.26  augustss 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
    904  1.26  augustss 		case SYNTH_SUB_MIDI_TYPE_MPU401:
    905  1.26  augustss 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
    906  1.26  augustss 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
    907  1.26  augustss 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
    908  1.26  augustss 		default:
    909  1.26  augustss 			osi.synth_subtype = 0; break;
    910  1.26  augustss 		}
    911  1.26  augustss 		osi.perc_mode = 0;
    912  1.26  augustss 		osi.nr_voices = si.nr_voices;
    913  1.26  augustss 		osi.nr_drums = 0;
    914  1.26  augustss 		osi.instr_bank_size = si.instr_bank_size;
    915  1.26  augustss 		osi.capabilities = 0;
    916  1.26  augustss 		if (si.capabilities & SYNTH_CAP_OPL3)
    917  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
    918  1.26  augustss 		if (si.capabilities & SYNTH_CAP_INPUT)
    919  1.26  augustss 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
    920  1.28   thorpej 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
    921  1.28   thorpej 		goto out;
    922  1.26  augustss 	case OSS_SEQ_CTRLRATE:
    923  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    924  1.26  augustss 		if (error)
    925  1.28   thorpej 			goto out;
    926  1.26  augustss 		error = ioctlf(fp, SEQUENCER_CTRLRATE, (caddr_t)&idat, p);
    927  1.26  augustss 		if (error)
    928  1.28   thorpej 			goto out;
    929  1.26  augustss 		retval[0] = idat;
    930  1.26  augustss 		break;
    931  1.26  augustss 	case OSS_SEQ_GETOUTCOUNT:
    932  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, (caddr_t)&idat, p);
    933  1.26  augustss 		if (error)
    934  1.28   thorpej 			goto out;
    935  1.26  augustss 		retval[0] = idat;
    936  1.26  augustss 		break;
    937  1.26  augustss 	case OSS_SEQ_GETINCOUNT:
    938  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, (caddr_t)&idat, p);
    939  1.26  augustss 		if (error)
    940  1.28   thorpej 			goto out;
    941  1.26  augustss 		retval[0] = idat;
    942  1.26  augustss 		break;
    943  1.26  augustss 	case OSS_SEQ_NRSYNTHS:
    944  1.26  augustss 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, (caddr_t)&idat, p);
    945  1.26  augustss 		if (error)
    946  1.28   thorpej 			goto out;
    947  1.26  augustss 		retval[0] = idat;
    948  1.26  augustss 		break;
    949  1.26  augustss 	case OSS_SEQ_NRMIDIS:
    950  1.26  augustss 		error = ioctlf(fp, SEQUENCER_NRMIDIS, (caddr_t)&idat, p);
    951  1.26  augustss 		if (error)
    952  1.28   thorpej 			goto out;
    953  1.26  augustss 		retval[0] = idat;
    954  1.26  augustss 		break;
    955  1.26  augustss 	case OSS_SEQ_THRESHOLD:
    956  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    957  1.26  augustss 		if (error)
    958  1.28   thorpej 			goto out;
    959  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_THRESHOLD, (caddr_t)&idat, p);
    960  1.28   thorpej 		goto out;
    961  1.26  augustss 	case OSS_MEMAVL:
    962  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    963  1.26  augustss 		if (error)
    964  1.28   thorpej 			goto out;
    965  1.26  augustss 		error = ioctlf(fp, SEQUENCER_MEMAVL, (caddr_t)&idat, p);
    966  1.26  augustss 		if (error)
    967  1.28   thorpej 			goto out;
    968  1.26  augustss 		retval[0] = idat;
    969  1.26  augustss 		break;
    970  1.26  augustss 	case OSS_SEQ_PANIC:
    971  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_PANIC, (caddr_t)&idat, p);
    972  1.28   thorpej 		goto out;
    973  1.26  augustss 	case OSS_SEQ_OUTOFBAND:
    974  1.26  augustss 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
    975  1.26  augustss 		if (error)
    976  1.28   thorpej 			goto out;
    977  1.26  augustss 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, (caddr_t)&oser, p);
    978  1.26  augustss 		if (error)
    979  1.28   thorpej 			goto out;
    980  1.26  augustss 		break;
    981  1.26  augustss 	case OSS_SEQ_GETTIME:
    982  1.26  augustss 		error = ioctlf(fp, SEQUENCER_GETTIME, (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_TIMEBASE:
    988  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
    989  1.26  augustss 		if (error)
    990  1.28   thorpej 			goto out;
    991  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, (caddr_t)&idat, p);
    992  1.26  augustss 		if (error)
    993  1.28   thorpej 			goto out;
    994  1.26  augustss 		retval[0] = idat;
    995  1.26  augustss 		break;
    996  1.26  augustss 	case OSS_TMR_START:
    997  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_START, (caddr_t)&idat, p);
    998  1.28   thorpej 		goto out;
    999  1.26  augustss 	case OSS_TMR_STOP:
   1000  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_STOP, (caddr_t)&idat, p);
   1001  1.28   thorpej 		goto out;
   1002  1.26  augustss 	case OSS_TMR_CONTINUE:
   1003  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, (caddr_t)&idat, p);
   1004  1.28   thorpej 		goto out;
   1005  1.26  augustss 	case OSS_TMR_TEMPO:
   1006  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1007  1.26  augustss 		if (error)
   1008  1.28   thorpej 			goto out;
   1009  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, (caddr_t)&idat, p);
   1010  1.26  augustss 		if (error)
   1011  1.28   thorpej 			goto out;
   1012  1.26  augustss 		retval[0] = idat;
   1013  1.26  augustss 		break;
   1014  1.26  augustss 	case OSS_TMR_SOURCE:
   1015  1.26  augustss 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
   1016  1.26  augustss 		if (error)
   1017  1.28   thorpej 			goto out;
   1018  1.26  augustss 		idat = 0;
   1019  1.26  augustss 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
   1020  1.26  augustss 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, (caddr_t)&idat, p);
   1021  1.26  augustss 		if (error)
   1022  1.28   thorpej 			goto out;
   1023  1.26  augustss 		idat1 = idat;
   1024  1.26  augustss 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
   1025  1.26  augustss 		retval[0] = idat;
   1026  1.26  augustss 		break;
   1027  1.26  augustss 	case OSS_TMR_METRONOME:
   1028  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1029  1.26  augustss 		if (error)
   1030  1.28   thorpej 			goto out;
   1031  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, (caddr_t)&idat, p);
   1032  1.28   thorpej 		goto out;
   1033  1.26  augustss 	case OSS_TMR_SELECT:
   1034  1.26  augustss 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
   1035  1.26  augustss 		if (error)
   1036  1.28   thorpej 			goto out;
   1037  1.26  augustss 		retval[0] = idat;
   1038  1.28   thorpej 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, (caddr_t)&idat, p);
   1039  1.28   thorpej 		goto out;
   1040  1.26  augustss 	default:
   1041  1.28   thorpej 		error = EINVAL;
   1042  1.28   thorpej 		goto out;
   1043  1.26  augustss 	}
   1044  1.26  augustss 
   1045  1.28   thorpej 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
   1046  1.28   thorpej  out:
   1047  1.28   thorpej 	FILE_UNUSE(fp, p);
   1048  1.28   thorpej 	return error;
   1049  1.14  augustss }
   1050  1.14  augustss 
   1051  1.14  augustss /*
   1052  1.14  augustss  * Check that the blocksize is a power of 2 as OSS wants.
   1053  1.14  augustss  * If not, set it to be.
   1054  1.14  augustss  */
   1055  1.14  augustss static void setblocksize(fp, info, p)
   1056  1.14  augustss 	struct file *fp;
   1057  1.14  augustss 	struct audio_info *info;
   1058  1.14  augustss 	struct proc *p;
   1059  1.14  augustss {
   1060  1.14  augustss 	struct audio_info set;
   1061  1.14  augustss 	int s;
   1062  1.14  augustss 
   1063  1.14  augustss 	if (info->blocksize & (info->blocksize-1)) {
   1064  1.14  augustss 		for(s = 32; s < info->blocksize; s <<= 1)
   1065  1.14  augustss 			;
   1066  1.14  augustss 		AUDIO_INITINFO(&set);
   1067  1.14  augustss 		set.blocksize = s;
   1068  1.14  augustss 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, (caddr_t)&set, p);
   1069  1.14  augustss 		fp->f_ops->fo_ioctl(fp, AUDIO_GETINFO, (caddr_t)info, p);
   1070  1.14  augustss 	}
   1071   1.1   mycroft }
   1072