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