xref: /netbsd-src/sys/compat/ossaudio/ossaudio.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: ossaudio.c,v 1.51 2006/05/14 21:24:50 elad 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.51 2006/05/14 21:24:50 elad 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 	struct proc *p = l->l_proc;
603 	mixer_devinfo_t mi;
604 	int i, j, e;
605 	static const struct {
606 		const char *name;
607 		int code;
608 	} *dp, devs[] = {
609 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
610 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
611 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
612 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
613 		{ AudioNaux,		OSS_SOUND_MIXER_LINE1 },
614 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
615 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
616 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
617 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
618 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
619 /*		{ AudioNheadphone,	?? },*/
620 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
621 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
622 /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
623 /*		{ AudioNstereo,		?? },*/
624 /*		{ AudioNmono,		?? },*/
625 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
626 /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
627 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
628 /*		{ AudioNmixerout,	?? },*/
629 		{ 0, -1 }
630 	};
631 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *) =
632 	    fp->f_ops->fo_ioctl;
633 	struct vnode *vp;
634 	struct vattr va;
635 	static struct audiodevinfo devcache = { 0 };
636 	struct audiodevinfo *di = &devcache;
637 	int mlen, dlen;
638 
639 	/*
640 	 * Figure out what device it is so we can check if the
641 	 * cached data is valid.
642 	 */
643 	vp = (struct vnode *)fp->f_data;
644 	if (vp->v_type != VCHR)
645 		return 0;
646 	if (VOP_GETATTR(vp, &va, p->p_cred, l))
647 		return 0;
648 	if (di->done && di->dev == va.va_rdev)
649 		return di;
650 
651 	di->done = 1;
652 	di->dev = va.va_rdev;
653 	di->devmask = 0;
654 	di->recmask = 0;
655 	di->stereomask = 0;
656 	di->source = ~0;
657 	di->caps = 0;
658 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
659 		di->devmap[i] = -1;
660 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
661 		di->rdevmap[i] = -1;
662 		di->names[i][0] = '\0';
663 		di->enum2opaque[i] = -1;
664 	}
665 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
666 		mi.index = i;
667 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, l) < 0)
668 			break;
669 		switch(mi.type) {
670 		case AUDIO_MIXER_VALUE:
671 			for(dp = devs; dp->name; dp++) {
672 				if (strcmp(dp->name, mi.label.name) == 0)
673 					break;
674 				dlen = strlen(dp->name);
675 				mlen = strlen(mi.label.name);
676 				if (dlen < mlen
677 				    && mi.label.name[mlen-dlen-1] == '.'
678 				    && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
679 					break;
680 			}
681 			if (dp->code >= 0) {
682 				di->devmap[dp->code] = i;
683 				di->rdevmap[i] = dp->code;
684 				di->devmask |= 1 << dp->code;
685 				if (mi.un.v.num_channels == 2)
686 					di->stereomask |= 1 << dp->code;
687 				strncpy(di->names[i], mi.label.name,
688 					sizeof di->names[i]);
689 			}
690 			break;
691 		}
692 	}
693 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
694 		mi.index = i;
695 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, l) < 0)
696 			break;
697 		if (strcmp(mi.label.name, AudioNsource) != 0)
698 			continue;
699 		di->source = i;
700 		switch(mi.type) {
701 		case AUDIO_MIXER_ENUM:
702 			for(j = 0; j < mi.un.e.num_mem; j++) {
703 				e = opaque_to_enum(di,
704 						   &mi.un.e.member[j].label,
705 						   mi.un.e.member[j].ord);
706 				if (e >= 0)
707 					di->recmask |= 1 << di->rdevmap[e];
708 			}
709 			di->caps = OSS_SOUND_CAP_EXCL_INPUT;
710 			break;
711 		case AUDIO_MIXER_SET:
712 			for(j = 0; j < mi.un.s.num_mem; j++) {
713 				e = opaque_to_enum(di,
714 						   &mi.un.s.member[j].label,
715 						   mi.un.s.member[j].mask);
716 				if (e >= 0)
717 					di->recmask |= 1 << di->rdevmap[e];
718 			}
719 			break;
720 		}
721 	}
722 	return di;
723 }
724 
725 int
726 oss_ioctl_mixer(lwp, uap, retval)
727 	struct lwp *lwp;
728 	struct oss_sys_ioctl_args /* {
729 		syscallarg(int) fd;
730 		syscallarg(u_long) com;
731 		syscallarg(caddr_t) data;
732 	} */ *uap;
733 	register_t *retval;
734 {
735 	struct proc *p = lwp->l_proc;
736 	struct file *fp;
737 	struct filedesc *fdp;
738 	u_long com;
739 	struct audiodevinfo *di;
740 	mixer_ctrl_t mc;
741 	struct oss_mixer_info omi;
742 	struct audio_device adev;
743 	int idat;
744 	int i;
745 	int error;
746 	int l, r, n, e;
747 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
748 
749 	fdp = p->p_fd;
750 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
751 		return (EBADF);
752 
753 	FILE_USE(fp);
754 
755 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
756 		error = EBADF;
757 		goto out;
758 	}
759 
760 	com = SCARG(uap, com);
761 	DPRINTF(("oss_ioctl_mixer: com=%08lx\n", com));
762 
763 	retval[0] = 0;
764 
765 	di = getdevinfo(fp, lwp);
766 	if (di == 0) {
767 		error = EINVAL;
768 		goto out;
769 	}
770 
771 	ioctlf = fp->f_ops->fo_ioctl;
772 	switch (com) {
773 	case OSS_GET_VERSION:
774 		idat = OSS_SOUND_VERSION;
775 		break;
776 	case OSS_SOUND_MIXER_INFO:
777 	case OSS_SOUND_OLD_MIXER_INFO:
778 		error = ioctlf(fp, AUDIO_GETDEV, (caddr_t)&adev, lwp);
779 		if (error)
780 			goto out;
781 		omi.modify_counter = 1;
782 		strncpy(omi.id, adev.name, sizeof omi.id);
783 		strncpy(omi.name, adev.name, sizeof omi.name);
784 		error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
785 		goto out;
786 	case OSS_SOUND_MIXER_READ_RECSRC:
787 		if (di->source == -1) {
788 			error = EINVAL;
789 			goto out;
790 		}
791 		mc.dev = di->source;
792 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
793 			mc.type = AUDIO_MIXER_ENUM;
794 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, lwp);
795 			if (error)
796 				goto out;
797 			e = opaque_to_enum(di, NULL, mc.un.ord);
798 			if (e >= 0)
799 				idat = 1 << di->rdevmap[e];
800 		} else {
801 			mc.type = AUDIO_MIXER_SET;
802 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, lwp);
803 			if (error)
804 				goto out;
805 			e = opaque_to_enum(di, NULL, mc.un.mask);
806 			if (e >= 0)
807 				idat = 1 << di->rdevmap[e];
808 		}
809 		break;
810 	case OSS_SOUND_MIXER_READ_DEVMASK:
811 		idat = di->devmask;
812 		break;
813 	case OSS_SOUND_MIXER_READ_RECMASK:
814 		idat = di->recmask;
815 		break;
816 	case OSS_SOUND_MIXER_READ_STEREODEVS:
817 		idat = di->stereomask;
818 		break;
819 	case OSS_SOUND_MIXER_READ_CAPS:
820 		idat = di->caps;
821 		break;
822 	case OSS_SOUND_MIXER_WRITE_RECSRC:
823 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
824 		if (di->source == -1) {
825 			error = EINVAL;
826 			goto out;
827 		}
828 		mc.dev = di->source;
829 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
830 		if (error)
831 			goto out;
832 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
833 			mc.type = AUDIO_MIXER_ENUM;
834 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
835 				if (idat & (1 << i))
836 					break;
837 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
838 			    di->devmap[i] == -1) {
839 				error = EINVAL;
840 				goto out;
841 			}
842 			mc.un.ord = enum_to_ord(di, di->devmap[i]);
843 		} else {
844 			mc.type = AUDIO_MIXER_SET;
845 			mc.un.mask = 0;
846 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
847 				if (idat & (1 << i)) {
848 					if (di->devmap[i] == -1) {
849 						error = EINVAL;
850 						goto out;
851 					}
852 					mc.un.mask |= enum_to_mask(di, di->devmap[i]);
853 				}
854 			}
855 		}
856 		error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, lwp);
857 		goto out;
858 	default:
859 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
860 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
861 			n = OSS_GET_DEV(com);
862 			if (di->devmap[n] == -1) {
863 				error = EINVAL;
864 				goto out;
865 			}
866 		    doread:
867 			mc.dev = di->devmap[n];
868 			mc.type = AUDIO_MIXER_VALUE;
869 			mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
870 			error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, lwp);
871 			if (error)
872 				goto out;
873 			if (mc.un.value.num_channels != 2) {
874 				l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
875 			} else {
876 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
877 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
878 			}
879 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
880 			DPRINTF(("OSS_MIXER_READ  n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
881 				 n, di->devmap[n], l, r, idat));
882 			break;
883 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
884 			   com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
885 			   (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
886 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
887 			n = OSS_GET_DEV(com);
888 			if (di->devmap[n] == -1) {
889 				error = EINVAL;
890 				goto out;
891 			}
892 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
893 			if (error)
894 				goto out;
895 			l = FROM_OSSVOL( idat       & 0xff);
896 			r = FROM_OSSVOL((idat >> 8) & 0xff);
897 			mc.dev = di->devmap[n];
898 			mc.type = AUDIO_MIXER_VALUE;
899 			if (di->stereomask & (1<<n)) {
900 				mc.un.value.num_channels = 2;
901 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
902 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
903 			} else {
904 				mc.un.value.num_channels = 1;
905 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
906 			}
907 			DPRINTF(("OSS_MIXER_WRITE n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
908 				 n, di->devmap[n], l, r, idat));
909 			error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, lwp);
910 			if (error)
911 				goto out;
912 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
913 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
914 				error = 0;
915 				goto out;
916 			}
917 			goto doread;
918 		} else {
919 #ifdef AUDIO_DEBUG
920 			printf("oss_audio: unknown mixer ioctl %04lx\n", com);
921 #endif
922 			error = EINVAL;
923 			goto out;
924 		}
925 	}
926 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
927  out:
928 	FILE_UNUSE(fp, lwp);
929 	return error;
930 }
931 
932 /* Sequencer emulation */
933 int
934 oss_ioctl_sequencer(l, uap, retval)
935 	struct lwp *l;
936 	struct oss_sys_ioctl_args /* {
937 		syscallarg(int) fd;
938 		syscallarg(u_long) com;
939 		syscallarg(caddr_t) data;
940 	} */ *uap;
941 	register_t *retval;
942 {
943 	struct proc *p = l->l_proc;
944 	struct file *fp;
945 	struct filedesc *fdp;
946 	u_long com;
947 	int idat, idat1;
948 	struct synth_info si;
949 	struct oss_synth_info osi;
950 	struct oss_seq_event_rec oser;
951 	int error;
952 	int (*ioctlf)(struct file *, u_long, void *, struct lwp *);
953 
954 	fdp = p->p_fd;
955 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
956 		return (EBADF);
957 
958 	FILE_USE(fp);
959 
960 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
961 		error = EBADF;
962 		goto out;
963 	}
964 
965 	com = SCARG(uap, com);
966 	DPRINTF(("oss_ioctl_sequencer: com=%08lx\n", com));
967 
968 	retval[0] = 0;
969 
970 	ioctlf = fp->f_ops->fo_ioctl;
971 	switch (com) {
972 	case OSS_SEQ_RESET:
973 		error = ioctlf(fp, SEQUENCER_RESET, (caddr_t)&idat, l);
974 		goto out;
975 	case OSS_SEQ_SYNC:
976 		error = ioctlf(fp, SEQUENCER_SYNC, (caddr_t)&idat, l);
977 		goto out;
978 	case OSS_SYNTH_INFO:
979 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
980 		if (error)
981 			goto out;
982 		si.device = osi.device;
983 		error = ioctlf(fp, SEQUENCER_INFO, (caddr_t)&si, l);
984 		if (error)
985 			goto out;
986 		strncpy(osi.name, si.name, sizeof osi.name);
987 		osi.device = si.device;
988 		switch(si.synth_type) {
989 		case SYNTH_TYPE_FM:
990 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
991 		case SYNTH_TYPE_SAMPLE:
992 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
993 		case SYNTH_TYPE_MIDI:
994 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
995 		default:
996 			osi.synth_type = 0; break;
997 		}
998 		switch(si.synth_subtype) {
999 		case SYNTH_SUB_FM_TYPE_ADLIB:
1000 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
1001 		case SYNTH_SUB_FM_TYPE_OPL3:
1002 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
1003 		case SYNTH_SUB_MIDI_TYPE_MPU401:
1004 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
1005 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
1006 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
1007 		default:
1008 			osi.synth_subtype = 0; break;
1009 		}
1010 		osi.perc_mode = 0;
1011 		osi.nr_voices = si.nr_voices;
1012 		osi.nr_drums = 0;
1013 		osi.instr_bank_size = si.instr_bank_size;
1014 		osi.capabilities = 0;
1015 		if (si.capabilities & SYNTH_CAP_OPL3)
1016 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
1017 		if (si.capabilities & SYNTH_CAP_INPUT)
1018 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
1019 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
1020 		goto out;
1021 	case OSS_SEQ_CTRLRATE:
1022 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1023 		if (error)
1024 			goto out;
1025 		error = ioctlf(fp, SEQUENCER_CTRLRATE, (caddr_t)&idat, l);
1026 		if (error)
1027 			goto out;
1028 		retval[0] = idat;
1029 		break;
1030 	case OSS_SEQ_GETOUTCOUNT:
1031 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, (caddr_t)&idat, l);
1032 		if (error)
1033 			goto out;
1034 		retval[0] = idat;
1035 		break;
1036 	case OSS_SEQ_GETINCOUNT:
1037 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, (caddr_t)&idat, l);
1038 		if (error)
1039 			goto out;
1040 		retval[0] = idat;
1041 		break;
1042 	case OSS_SEQ_NRSYNTHS:
1043 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, (caddr_t)&idat, l);
1044 		if (error)
1045 			goto out;
1046 		retval[0] = idat;
1047 		break;
1048 	case OSS_SEQ_NRMIDIS:
1049 		error = ioctlf(fp, SEQUENCER_NRMIDIS, (caddr_t)&idat, l);
1050 		if (error)
1051 			goto out;
1052 		retval[0] = idat;
1053 		break;
1054 	case OSS_SEQ_THRESHOLD:
1055 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1056 		if (error)
1057 			goto out;
1058 		error = ioctlf(fp, SEQUENCER_THRESHOLD, (caddr_t)&idat, l);
1059 		goto out;
1060 	case OSS_MEMAVL:
1061 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1062 		if (error)
1063 			goto out;
1064 		error = ioctlf(fp, SEQUENCER_MEMAVL, (caddr_t)&idat, l);
1065 		if (error)
1066 			goto out;
1067 		retval[0] = idat;
1068 		break;
1069 	case OSS_SEQ_PANIC:
1070 		error = ioctlf(fp, SEQUENCER_PANIC, (caddr_t)&idat, l);
1071 		goto out;
1072 	case OSS_SEQ_OUTOFBAND:
1073 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
1074 		if (error)
1075 			goto out;
1076 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, (caddr_t)&oser, l);
1077 		if (error)
1078 			goto out;
1079 		break;
1080 	case OSS_SEQ_GETTIME:
1081 		error = ioctlf(fp, SEQUENCER_GETTIME, (caddr_t)&idat, l);
1082 		if (error)
1083 			goto out;
1084 		retval[0] = idat;
1085 		break;
1086 	case OSS_TMR_TIMEBASE:
1087 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1088 		if (error)
1089 			goto out;
1090 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, (caddr_t)&idat, l);
1091 		if (error)
1092 			goto out;
1093 		retval[0] = idat;
1094 		break;
1095 	case OSS_TMR_START:
1096 		error = ioctlf(fp, SEQUENCER_TMR_START, (caddr_t)&idat, l);
1097 		goto out;
1098 	case OSS_TMR_STOP:
1099 		error = ioctlf(fp, SEQUENCER_TMR_STOP, (caddr_t)&idat, l);
1100 		goto out;
1101 	case OSS_TMR_CONTINUE:
1102 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, (caddr_t)&idat, l);
1103 		goto out;
1104 	case OSS_TMR_TEMPO:
1105 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1106 		if (error)
1107 			goto out;
1108 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, (caddr_t)&idat, l);
1109 		if (error)
1110 			goto out;
1111 		retval[0] = idat;
1112 		break;
1113 	case OSS_TMR_SOURCE:
1114 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
1115 		if (error)
1116 			goto out;
1117 		idat = 0;
1118 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
1119 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, (caddr_t)&idat, l);
1120 		if (error)
1121 			goto out;
1122 		idat1 = idat;
1123 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
1124 		retval[0] = idat;
1125 		break;
1126 	case OSS_TMR_METRONOME:
1127 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1128 		if (error)
1129 			goto out;
1130 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, (caddr_t)&idat, l);
1131 		goto out;
1132 	case OSS_TMR_SELECT:
1133 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1134 		if (error)
1135 			goto out;
1136 		retval[0] = idat;
1137 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, (caddr_t)&idat, l);
1138 		goto out;
1139 	default:
1140 		error = EINVAL;
1141 		goto out;
1142 	}
1143 
1144 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
1145  out:
1146 	FILE_UNUSE(fp, l);
1147 	return error;
1148 }
1149 
1150 /*
1151  * Check that the blocksize is a power of 2 as OSS wants.
1152  * If not, set it to be.
1153  */
1154 static void
1155 setblocksize(fp, info, l)
1156 	struct file *fp;
1157 	struct audio_info *info;
1158 	struct lwp *l;
1159 {
1160 	struct audio_info set;
1161 	int s;
1162 
1163 	 if (info->blocksize & (info->blocksize-1)) {
1164 		for(s = 32; s < info->blocksize; s <<= 1)
1165 			;
1166 		AUDIO_INITINFO(&set);
1167 		set.blocksize = s;
1168 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, (caddr_t)&set, l);
1169 		fp->f_ops->fo_ioctl(fp, AUDIO_GETINFO, (caddr_t)info, l);
1170 	}
1171 }
1172