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