xref: /netbsd-src/sys/compat/ossaudio/ossaudio.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: ossaudio.c,v 1.83 2020/04/19 21:37:00 nia Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 2008 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ossaudio.c,v 1.83 2020/04/19 21:37:00 nia Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/proc.h>
34 #include <sys/systm.h>
35 #include <sys/file.h>
36 #include <sys/vnode.h>
37 #include <sys/filedesc.h>
38 #include <sys/ioctl.h>
39 #include <sys/mount.h>
40 #include <sys/kernel.h>
41 #include <sys/audioio.h>
42 #include <sys/midiio.h>
43 #include <sys/kauth.h>
44 #include <sys/syscallargs.h>
45 #include <sys/module.h>
46 
47 #include <compat/ossaudio/ossaudio.h>
48 #include <compat/ossaudio/ossaudiovar.h>
49 
50 MODULE(MODULE_CLASS_EXEC, compat_ossaudio, NULL);
51 
52 #ifdef AUDIO_DEBUG
53 #define DPRINTF(x) if (ossdebug) printf x
54 int ossdebug = 0;
55 #else
56 #define DPRINTF(x)
57 #endif
58 
59 #define TO_OSSVOL(x)	(((x) * 100 + 127) / 255)
60 #define FROM_OSSVOL(x)	((((x) > 100 ? 100 : (x)) * 255 + 50) / 100)
61 
62 #define GETPRINFO(info, name)	\
63 	(((info)->mode == AUMODE_RECORD) \
64 	    ? (info)->record.name : (info)->play.name)
65 
66 static struct audiodevinfo *getdevinfo(file_t *);
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 setchannels(file_t *, int, int);
72 static void setblocksize(file_t *, struct audio_info *);
73 
74 #ifdef AUDIO_DEBUG
75 static const char *
76 compat_ossaudio_getcmd(u_long cmd)
77 {
78 	static char buf[64];
79 	switch (cmd) {
80 #define _DO(_a) \
81 	case _a: \
82 		return # _a;
83 _DO(OSS_SNDCTL_DSP_RESET)
84 _DO(OSS_SNDCTL_DSP_SYNC)
85 _DO(OSS_SNDCTL_DSP_SPEED)
86 _DO(OSS_SOUND_PCM_READ_RATE)
87 _DO(OSS_SNDCTL_DSP_STEREO)
88 _DO(OSS_SNDCTL_DSP_GETBLKSIZE)
89 _DO(OSS_SNDCTL_DSP_SETFMT)
90 _DO(OSS_SOUND_PCM_READ_BITS)
91 _DO(OSS_SNDCTL_DSP_CHANNELS)
92 _DO(OSS_SOUND_PCM_READ_CHANNELS)
93 _DO(OSS_SOUND_PCM_WRITE_FILTER)
94 _DO(OSS_SOUND_PCM_READ_FILTER)
95 _DO(OSS_SNDCTL_DSP_POST)
96 _DO(OSS_SNDCTL_DSP_SUBDIVIDE)
97 _DO(OSS_SNDCTL_DSP_SETFRAGMENT)
98 _DO(OSS_SNDCTL_DSP_GETFMTS)
99 _DO(OSS_SNDCTL_DSP_GETOSPACE)
100 _DO(OSS_SNDCTL_DSP_GETISPACE)
101 _DO(OSS_SNDCTL_DSP_NONBLOCK)
102 _DO(OSS_SNDCTL_DSP_GETCAPS)
103 _DO(OSS_SNDCTL_DSP_GETTRIGGER)
104 _DO(OSS_SNDCTL_DSP_SETTRIGGER)
105 _DO(OSS_SNDCTL_DSP_GETIPTR)
106 _DO(OSS_SNDCTL_DSP_GETOPTR)
107 _DO(OSS_SNDCTL_DSP_MAPINBUF)
108 _DO(OSS_SNDCTL_DSP_MAPOUTBUF)
109 _DO(OSS_SNDCTL_DSP_SETSYNCRO)
110 _DO(OSS_SNDCTL_DSP_SETDUPLEX)
111 _DO(OSS_SNDCTL_DSP_GETODELAY)
112 _DO(OSS_SNDCTL_DSP_PROFILE)
113 _DO(OSS_SOUND_MIXER_INFO)
114 _DO(OSS_SOUND_OLD_MIXER_INFO)
115 _DO(OSS_GET_VERSION)
116 _DO(OSS_SEQ_RESET)
117 _DO(OSS_SEQ_SYNC)
118 _DO(OSS_SYNTH_INFO)
119 _DO(OSS_SEQ_CTRLRATE)
120 _DO(OSS_SEQ_GETOUTCOUNT)
121 _DO(OSS_SEQ_GETINCOUNT)
122 _DO(OSS_SEQ_PERCMODE)
123 _DO(OSS_SEQ_TESTMIDI)
124 _DO(OSS_SEQ_RESETSAMPLES)
125 _DO(OSS_SEQ_NRSYNTHS)
126 _DO(OSS_SEQ_NRMIDIS)
127 #ifdef notyet
128 _DO(OSS_MIDI_INFO)
129 #endif
130 _DO(OSS_SEQ_THRESHOLD)
131 _DO(OSS_MEMAVL)
132 _DO(OSS_FM_4OP_ENABLE)
133 _DO(OSS_SEQ_PANIC)
134 _DO(OSS_SEQ_OUTOFBAND)
135 _DO(OSS_SEQ_GETTIME)
136 _DO(OSS_ID)
137 _DO(OSS_CONTROL)
138 _DO(OSS_REMOVESAMPLE)
139 _DO(OSS_TMR_TIMEBASE)
140 _DO(OSS_TMR_START)
141 _DO(OSS_TMR_STOP)
142 _DO(OSS_TMR_CONTINUE)
143 _DO(OSS_TMR_TEMPO)
144 _DO(OSS_TMR_SOURCE)
145 _DO(OSS_TMR_METRONOME)
146 _DO(OSS_TMR_SELECT)
147 #undef _DO
148 	default:
149 		(void)snprintf(buf, sizeof(buf), "*0x%lx*", cmd);
150 		return buf;
151 	}
152 }
153 #endif
154 
155 
156 static int
157 compat_ossaudio_modcmd(modcmd_t cmd, void *arg)
158 {
159 
160 	switch (cmd) {
161 	case MODULE_CMD_INIT:
162 	case MODULE_CMD_FINI:
163 		return 0;
164 	default:
165 		return ENOTTY;
166 	}
167 }
168 
169 int
170 oss_ioctl_audio(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
171 {
172 	/* {
173 		syscallarg(int) fd;
174 		syscallarg(u_long) com;
175 		syscallarg(void *) data;
176 	} */
177 	file_t *fp;
178 	u_long com;
179 	struct audio_info tmpinfo, hwfmt;
180 	struct audio_offset tmpoffs;
181 	struct oss_audio_buf_info bufinfo;
182 	struct oss_count_info cntinfo;
183 	struct audio_encoding tmpenc;
184 	u_int u;
185 	u_int encoding;
186 	u_int precision;
187 	int idat, idata;
188 	int error = 0;
189 	int (*ioctlf)(file_t *, u_long, void *);
190 
191 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
192 		return (EBADF);
193 
194 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
195 		error = EBADF;
196 		goto out;
197 	}
198 
199 	com = SCARG(uap, com);
200 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
201 
202 	retval[0] = 0;
203 
204 	ioctlf = fp->f_ops->fo_ioctl;
205 	switch (com) {
206 	case OSS_SNDCTL_DSP_RESET:
207 		error = ioctlf(fp, AUDIO_FLUSH, NULL);
208 		if (error) {
209 			DPRINTF(("%s: AUDIO_FLUSH %d\n", __func__, error));
210 			goto out;
211 		}
212 		break;
213 	case OSS_SNDCTL_DSP_SYNC:
214 		error = ioctlf(fp, AUDIO_DRAIN, NULL);
215 		if (error) {
216 			DPRINTF(("%s: AUDIO_DRAIN %d\n", __func__, error));
217 			goto out;
218 		}
219 		break;
220 	case OSS_SNDCTL_DSP_POST:
221 		/* This call is merely advisory, and may be a nop. */
222 		break;
223 	case OSS_SNDCTL_DSP_SPEED:
224 		AUDIO_INITINFO(&tmpinfo);
225 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
226 		if (error) {
227 			DPRINTF(("%s: SNDCTL_DSP_SPEED %d\n",
228 			     __func__, error));
229 			goto out;
230 		}
231 		tmpinfo.play.sample_rate =
232 		tmpinfo.record.sample_rate = idat;
233 		DPRINTF(("%s: SNDCTL_DSP_SPEED > %d\n", __func__, idat));
234 		/*
235 		 * The default NetBSD behavior if an unsupported sample rate
236 		 * is set is to return an error code and keep the rate at the
237 		 * default of 8000 Hz.
238 		 *
239 		 * However, the OSS expectation is a sample rate supported by
240 		 * the hardware is returned if the exact rate could not be set.
241 		 *
242 		 * So, if the chosen sample rate is invalid, set and return
243 		 * the current hardware rate.
244 		 */
245 		if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) != 0) {
246 			error = ioctlf(fp, AUDIO_GETFORMAT, &hwfmt);
247 			if (error) {
248 				DPRINTF(("%s: AUDIO_GETFORMAT %d\n",
249 				     __func__, error));
250 				goto out;
251 			}
252 			error = ioctlf(fp, AUDIO_GETINFO, &tmpinfo);
253 			if (error) {
254 				DPRINTF(("%s: AUDIO_GETINFO %d\n",
255 				     __func__, error));
256 				goto out;
257 			}
258 			tmpinfo.play.sample_rate =
259 			tmpinfo.record.sample_rate =
260 			    (tmpinfo.mode == AUMODE_RECORD) ?
261 			    hwfmt.record.sample_rate :
262 			    hwfmt.play.sample_rate;
263 			error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
264 			if (error) {
265 				DPRINTF(("%s: SNDCTL_DSP_SPEED %d = %d\n",
266 				     __func__, idat, error));
267 				goto out;
268 			}
269 		}
270 		/* FALLTHROUGH */
271 	case OSS_SOUND_PCM_READ_RATE:
272 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
273 		if (error) {
274 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
275 			 __func__, error));
276 			goto out;
277 		}
278 		idat = GETPRINFO(&tmpinfo, sample_rate);
279 		DPRINTF(("%s: SNDCTL_PCM_READ_RATE < %d\n", __func__, idat));
280 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
281 		if (error) {
282 			DPRINTF(("%s: SOUND_PCM_READ_RATE %d = %d\n",
283 			     __func__, idat, error));
284 			goto out;
285 		}
286 		break;
287 	case OSS_SNDCTL_DSP_STEREO:
288 		AUDIO_INITINFO(&tmpinfo);
289 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
290 		if (error) {
291 			DPRINTF(("%s: SNDCTL_DSP_STEREO %d\n",
292 			     __func__, error));
293 			goto out;
294 		}
295 		tmpinfo.play.channels =
296 		tmpinfo.record.channels = idat ? 2 : 1;
297 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
298 		if (error) {
299 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
300 			     __func__, error));
301 			goto out;
302 		}
303 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
304 		if (error) {
305 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
306 			     __func__, error));
307 			goto out;
308 		}
309 		idat = GETPRINFO(&tmpinfo, channels) - 1;
310 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
311 		if (error) {
312 			DPRINTF(("%s: SNDCTL_DSP_STEREO %d = %d\n",
313 			     __func__, idat, error));
314 			goto out;
315 		}
316 		break;
317 	case OSS_SNDCTL_DSP_GETBLKSIZE:
318 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
319 		if (error) {
320 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
321 			     __func__, error));
322 			goto out;
323 		}
324 		setblocksize(fp, &tmpinfo);
325 		idat = tmpinfo.blocksize;
326 		DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE < %d\n",
327 		     __func__, idat));
328 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
329 		if (error) {
330 			DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE %d = %d\n",
331 			     __func__, idat, error));
332 			goto out;
333 		}
334 		break;
335 	case OSS_SNDCTL_DSP_SETFMT:
336 		AUDIO_INITINFO(&tmpinfo);
337 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
338 		if (error) {
339 			DPRINTF(("%s: SNDCTL_DSP_SETFMT %d\n",
340 			     __func__, error));
341 			goto out;
342 		}
343 		switch (idat) {
344 		case OSS_AFMT_MU_LAW:
345 			tmpinfo.play.precision =
346 			tmpinfo.record.precision = 8;
347 			tmpinfo.play.encoding =
348 			tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
349 			break;
350 		case OSS_AFMT_A_LAW:
351 			tmpinfo.play.precision =
352 			tmpinfo.record.precision = 8;
353 			tmpinfo.play.encoding =
354 			tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
355 			break;
356 		case OSS_AFMT_U8:
357 			tmpinfo.play.precision =
358 			tmpinfo.record.precision = 8;
359 			tmpinfo.play.encoding =
360 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
361 			break;
362 		case OSS_AFMT_S8:
363 			tmpinfo.play.precision =
364 			tmpinfo.record.precision = 8;
365 			tmpinfo.play.encoding =
366 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
367 			break;
368 		case OSS_AFMT_S16_LE:
369 			tmpinfo.play.precision =
370 			tmpinfo.record.precision = 16;
371 			tmpinfo.play.encoding =
372 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
373 			break;
374 		case OSS_AFMT_S16_BE:
375 			tmpinfo.play.precision =
376 			tmpinfo.record.precision = 16;
377 			tmpinfo.play.encoding =
378 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
379 			break;
380 		case OSS_AFMT_U16_LE:
381 			tmpinfo.play.precision =
382 			tmpinfo.record.precision = 16;
383 			tmpinfo.play.encoding =
384 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
385 			break;
386 		case OSS_AFMT_U16_BE:
387 			tmpinfo.play.precision =
388 			tmpinfo.record.precision = 16;
389 			tmpinfo.play.encoding =
390 			tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
391 			break;
392 		case OSS_AFMT_AC3:
393 			tmpinfo.play.precision =
394 			tmpinfo.record.precision = 16;
395 			tmpinfo.play.encoding =
396 			tmpinfo.record.encoding = AUDIO_ENCODING_AC3;
397 			break;
398 		default:
399 			/*
400 			 * OSSv4 specifies that if an invalid format is chosen
401 			 * by an application then a sensible format supported
402 			 * by the hardware is returned.
403 			 *
404 			 * In this case, we pick S16LE since it's reasonably
405 			 * assumed to be supported by applications.
406 			 */
407 			tmpinfo.play.precision =
408 			tmpinfo.record.precision = 16;
409 			tmpinfo.play.encoding =
410 			tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
411 			break;
412 		}
413 		DPRINTF(("%s: SNDCTL_DSP_SETFMT > 0x%x\n",
414 		    __func__, idat));
415 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
416 		if (error) {
417 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
418 			     __func__, error));
419 			goto out;
420 		}
421 		/* FALLTHROUGH */
422 	case OSS_SOUND_PCM_READ_BITS:
423 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
424 		if (error) {
425 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
426 			     __func__, error));
427 			goto out;
428 		}
429 		encoding = GETPRINFO(&tmpinfo, encoding);
430 		precision = GETPRINFO(&tmpinfo, precision);
431 		switch (encoding) {
432 		case AUDIO_ENCODING_ULAW:
433 			idat = OSS_AFMT_MU_LAW;
434 			break;
435 		case AUDIO_ENCODING_ALAW:
436 			idat = OSS_AFMT_A_LAW;
437 			break;
438 		case AUDIO_ENCODING_SLINEAR_LE:
439 			if (precision == 16)
440 				idat = OSS_AFMT_S16_LE;
441 			else
442 				idat = OSS_AFMT_S8;
443 			break;
444 		case AUDIO_ENCODING_SLINEAR_BE:
445 			if (precision == 16)
446 				idat = OSS_AFMT_S16_BE;
447 			else
448 				idat = OSS_AFMT_S8;
449 			break;
450 		case AUDIO_ENCODING_ULINEAR_LE:
451 			if (precision == 16)
452 				idat = OSS_AFMT_U16_LE;
453 			else
454 				idat = OSS_AFMT_U8;
455 			break;
456 		case AUDIO_ENCODING_ULINEAR_BE:
457 			if (precision == 16)
458 				idat = OSS_AFMT_U16_BE;
459 			else
460 				idat = OSS_AFMT_U8;
461 			break;
462 		case AUDIO_ENCODING_ADPCM:
463 			idat = OSS_AFMT_IMA_ADPCM;
464 			break;
465 		case AUDIO_ENCODING_AC3:
466 			idat = OSS_AFMT_AC3;
467 			break;
468 		default:
469 			DPRINTF(("%s: SOUND_PCM_READ_BITS bad encoding %d\n",
470 			     __func__, tmpinfo.play.encoding));
471 			error = EINVAL;
472 			goto out;
473 		}
474 		DPRINTF(("%s: SOUND_PCM_READ_BITS < 0x%x\n",
475 		    __func__, idat));
476 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
477 		if (error) {
478 			DPRINTF(("%s: SOUND_PCM_READ_BITS %d = %d\n",
479 			     __func__, idat, error));
480 			goto out;
481 		}
482 		break;
483 	case OSS_SNDCTL_DSP_CHANNELS:
484 		AUDIO_INITINFO(&tmpinfo);
485 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
486 		if (error) {
487 			DPRINTF(("%s: SNDCTL_DSP_CHANNELS %d\n",
488 			     __func__, error));
489 			goto out;
490 		}
491 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
492 		if (error) {
493 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
494 			     __func__, error));
495 			goto out;
496 		}
497 		setchannels(fp, tmpinfo.mode, idat);
498 		/* FALLTHROUGH */
499 	case OSS_SOUND_PCM_READ_CHANNELS:
500 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
501 		if (error) {
502 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
503 			     __func__, error));
504 			goto out;
505 		}
506 		idat = GETPRINFO(&tmpinfo, channels);
507 		DPRINTF(("%s: SOUND_PCM_READ_CHANNELS < %d\n", __func__, idat));
508 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
509 		if (error) {
510 			DPRINTF(("%s: SOUND_PCM_READ_CHANNELS %d = %d\n",
511 			     __func__, idat, error));
512 			goto out;
513 		}
514 		break;
515 	case OSS_SOUND_PCM_WRITE_FILTER:
516 	case OSS_SOUND_PCM_READ_FILTER:
517 		error = EINVAL; /* XXX unimplemented */
518 		DPRINTF(("%s: SOUND_PCM_{READ,WRITE}_FILTER filter\n",
519 		     __func__));
520 		goto out;
521 	case OSS_SNDCTL_DSP_SUBDIVIDE:
522 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
523 		if (error) {
524 			DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d\n",
525 			     __func__, error));
526 			goto out;
527 		}
528 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
529 		if (error) {
530 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
531 			     __func__, error));
532 			goto out;
533 		}
534 		setblocksize(fp, &tmpinfo);
535 		if (idat == 0)
536 			idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
537 		idat = (tmpinfo.play.buffer_size / idat) & -4;
538 		AUDIO_INITINFO(&tmpinfo);
539 		tmpinfo.blocksize = idat;
540 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
541 		if (error) {
542 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
543 			     __func__, error));
544 			goto out;
545 		}
546 		idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
547 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
548 		if (error) {
549 			DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d = %d\n",
550 			     __func__, idat, error));
551 			goto out;
552 		}
553 		break;
554 	case OSS_SNDCTL_DSP_SETFRAGMENT:
555 		AUDIO_INITINFO(&tmpinfo);
556 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
557 		if (error) {
558 			DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT %d\n",
559 			     __func__, error));
560 			goto out;
561 		}
562 		if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
563 			DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT bad ival%d\n",
564 			     __func__, idat));
565 			error = EINVAL;
566 			goto out;
567 		}
568 		tmpinfo.blocksize = 1 << (idat & 0xffff);
569 		tmpinfo.hiwat = (idat >> 16) & 0x7fff;
570 		DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT blksize=%d, "
571 		    "hiwat=%d\n", __func__, tmpinfo.blocksize, tmpinfo.hiwat));
572 		if (tmpinfo.hiwat == 0)	/* 0 means set to max */
573 			tmpinfo.hiwat = 65536;
574 		error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
575 		if (error) {
576 			DPRINTF(("%s: AUDIO_SETINFO %d\n",
577 			     __func__, error));
578 			goto out;
579 		}
580 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
581 		if (error) {
582 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
583 			     __func__, error));
584 			goto out;
585 		}
586 		u = tmpinfo.blocksize;
587 		for(idat = 0; u > 1; idat++, u >>= 1)
588 			;
589 		idat |= (tmpinfo.hiwat & 0x7fff) << 16;
590 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
591 		if (error) {
592 			DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT  %d = %d\n",
593 			     __func__, idat, error));
594 			goto out;
595 		}
596 		break;
597 	case OSS_SNDCTL_DSP_GETFMTS:
598 		for (idat = 0, tmpenc.index = 0;
599 		    ioctlf(fp, AUDIO_GETENC, &tmpenc) == 0;
600 		    tmpenc.index++) {
601 			switch(tmpenc.encoding) {
602 			case AUDIO_ENCODING_ULAW:
603 				idat |= OSS_AFMT_MU_LAW;
604 				break;
605 			case AUDIO_ENCODING_ALAW:
606 				idat |= OSS_AFMT_A_LAW;
607 				break;
608 			case AUDIO_ENCODING_SLINEAR:
609 				idat |= OSS_AFMT_S8;
610 				break;
611 			case AUDIO_ENCODING_SLINEAR_LE:
612 				if (tmpenc.precision == 16)
613 					idat |= OSS_AFMT_S16_LE;
614 				else
615 					idat |= OSS_AFMT_S8;
616 				break;
617 			case AUDIO_ENCODING_SLINEAR_BE:
618 				if (tmpenc.precision == 16)
619 					idat |= OSS_AFMT_S16_BE;
620 				else
621 					idat |= OSS_AFMT_S8;
622 				break;
623 			case AUDIO_ENCODING_ULINEAR:
624 				idat |= OSS_AFMT_U8;
625 				break;
626 			case AUDIO_ENCODING_ULINEAR_LE:
627 				if (tmpenc.precision == 16)
628 					idat |= OSS_AFMT_U16_LE;
629 				else
630 					idat |= OSS_AFMT_U8;
631 				break;
632 			case AUDIO_ENCODING_ULINEAR_BE:
633 				if (tmpenc.precision == 16)
634 					idat |= OSS_AFMT_U16_BE;
635 				else
636 					idat |= OSS_AFMT_U8;
637 				break;
638 			case AUDIO_ENCODING_ADPCM:
639 				idat |= OSS_AFMT_IMA_ADPCM;
640 				break;
641 			case AUDIO_ENCODING_AC3:
642 				idat |= OSS_AFMT_AC3;
643 				break;
644 			default:
645 				DPRINTF(("%s: SNDCTL_DSP_GETFMTS unknown %d\n",
646 				    __func__, tmpenc.encoding));
647 				break;
648 			}
649 		}
650 		DPRINTF(("%s: SNDCTL_DSP_GETFMTS < 0x%x\n",
651 		    __func__, idat));
652 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
653 		if (error) {
654 			DPRINTF(("%s: SNDCTL_DSP_GETFMTS = %x = %d\n",
655 			    __func__, idat, error));
656 			goto out;
657 		}
658 		break;
659 	case OSS_SNDCTL_DSP_GETOSPACE:
660 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
661 		if (error) {
662 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
663 			     __func__, error));
664 			goto out;
665 		}
666 		setblocksize(fp, &tmpinfo);
667 		bufinfo.fragsize = tmpinfo.blocksize;
668 		bufinfo.fragments = tmpinfo.hiwat -
669 		    (tmpinfo.play.seek + tmpinfo.blocksize - 1) /
670 		    tmpinfo.blocksize;
671 		bufinfo.fragstotal = tmpinfo.hiwat;
672 		bufinfo.bytes =
673 		    tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
674 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
675 		if (error) {
676 			DPRINTF(("%s: SNDCTL_DSP_GETOSPACE = %d\n",
677 			    __func__, error));
678 			goto out;
679 		}
680 		break;
681 	case OSS_SNDCTL_DSP_GETISPACE:
682 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
683 		if (error) {
684 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
685 			     __func__, error));
686 			goto out;
687 		}
688 		setblocksize(fp, &tmpinfo);
689 		bufinfo.fragsize = tmpinfo.blocksize;
690 		bufinfo.fragments = tmpinfo.record.seek / tmpinfo.blocksize;
691 		bufinfo.fragstotal =
692 		    tmpinfo.record.buffer_size / tmpinfo.blocksize;
693 		bufinfo.bytes = tmpinfo.record.seek;
694 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
695 		if (error) {
696 			DPRINTF(("%s: SNDCTL_DSP_GETISPACE %d %d %d %d = %d\n",
697 			     __func__, bufinfo.fragsize, bufinfo.fragments,
698 			     bufinfo.fragstotal, bufinfo.bytes, error));
699 			goto out;
700 		}
701 		break;
702 	case OSS_SNDCTL_DSP_NONBLOCK:
703 		idat = 1;
704 		error = ioctlf(fp, FIONBIO, &idat);
705 		if (error) {
706 			DPRINTF(("%s: FIONBIO %d\n",
707 			     __func__, error));
708 			goto out;
709 		}
710 		break;
711 	case OSS_SNDCTL_DSP_GETCAPS:
712 		error = ioctlf(fp, AUDIO_GETPROPS, &idata);
713 		if (error) {
714 			DPRINTF(("%s: AUDIO_GETPROPS %d\n",
715 			     __func__, error));
716 			goto out;
717 		}
718 		idat = OSS_DSP_CAP_TRIGGER;
719 		if (idata & AUDIO_PROP_FULLDUPLEX)
720 			idat |= OSS_DSP_CAP_DUPLEX;
721 		if (idata & AUDIO_PROP_MMAP)
722 			idat |= OSS_DSP_CAP_MMAP;
723 		DPRINTF(("%s: SNDCL_DSP_GETCAPS %s duplex, %smmap\n",
724 		     __func__, (idat & OSS_DSP_CAP_DUPLEX) ? "full" : "half",
725 		     (idat & OSS_DSP_CAP_MMAP) ? "" : "no "));
726 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
727 		if (error) {
728 			DPRINTF(("%s: SNDCTL_DSP_GETCAPS %x = %d\n", __func__,
729 			    idat, error));
730 			goto out;
731 		}
732 		break;
733 	case OSS_SNDCTL_DSP_SETTRIGGER:
734 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
735 		if (error) {
736 			DPRINTF(("%s: SNDCTL_DSP_SETTRIGGER: %d\n",
737 			     __func__, error));
738 			goto out;
739 		}
740 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
741 		if (error) {
742 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
743 			     __func__, error));
744 			goto out;
745 		}
746 		AUDIO_INITINFO(&tmpinfo);
747 		if (tmpinfo.mode & AUMODE_PLAY)
748 			tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
749 		if (tmpinfo.mode & AUMODE_RECORD)
750 			tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
751 		(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
752 		/* FALLTHRU */
753 	case OSS_SNDCTL_DSP_GETTRIGGER:
754 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
755 		if (error) {
756 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
757 			     __func__, error));
758 			goto out;
759 		}
760 		idat = 0;
761 		if ((tmpinfo.mode & AUMODE_PLAY) && !tmpinfo.play.pause)
762 			idat |= OSS_PCM_ENABLE_OUTPUT;
763 		if ((tmpinfo.mode & AUMODE_RECORD) && !tmpinfo.record.pause)
764 			idat |= OSS_PCM_ENABLE_INPUT;
765 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
766 		if (error) {
767 			DPRINTF(("%s: SNDCTL_DSP_GETTRIGGER = %x = %d\n",
768 			    __func__, idat, error));
769 			goto out;
770 		}
771 		break;
772 	case OSS_SNDCTL_DSP_GETIPTR:
773 		error = ioctlf(fp, AUDIO_GETIOFFS, &tmpoffs);
774 		if (error) {
775 			DPRINTF(("%s: AUDIO_GETIOFFS %d\n",
776 			     __func__, error));
777 			goto out;
778 		}
779 		cntinfo.bytes = tmpoffs.samples;
780 		cntinfo.blocks = tmpoffs.deltablks;
781 		cntinfo.ptr = tmpoffs.offset;
782 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
783 		if (error) {
784 			DPRINTF(("%s: SNDCTL_DSP_GETIPTR %d\n",
785 			    __func__, error));
786 			goto out;
787 		}
788 		break;
789 	case OSS_SNDCTL_DSP_GETOPTR:
790 		error = ioctlf(fp, AUDIO_GETOOFFS, &tmpoffs);
791 		if (error) {
792 			DPRINTF(("%s: AUDIO_GETOOFFS %d\n",
793 			     __func__, error));
794 			goto out;
795 		}
796 		cntinfo.bytes = tmpoffs.samples;
797 		cntinfo.blocks = tmpoffs.deltablks;
798 		cntinfo.ptr = tmpoffs.offset;
799 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
800 		if (error) {
801 			DPRINTF(("%s: SNDCTL_DSP_GETOPTR %d\n",
802 			    __func__, error));
803 			goto out;
804 		}
805 		break;
806 	case OSS_SNDCTL_DSP_SETDUPLEX:
807 		idat = 1;
808 		error = ioctlf(fp, AUDIO_SETFD, &idat);
809 		if (error) {
810 			DPRINTF(("%s: AUDIO_SETFD %d = %d\n",
811 			    __func__, idat, error));
812 			goto out;
813 		}
814 		break;
815 	case OSS_SNDCTL_DSP_MAPINBUF:
816 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPINBUF\n",
817 		    __func__));
818 		error = EINVAL;
819 		goto out;
820 	case OSS_SNDCTL_DSP_MAPOUTBUF:
821 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPOUTBUF\n",
822 		    __func__));
823 		error = EINVAL;
824 		goto out;
825 	case OSS_SNDCTL_DSP_SETSYNCRO:
826 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_GETSYNCHRO\n",
827 		    __func__));
828 		error = EINVAL;
829 		goto out;
830 	case OSS_SNDCTL_DSP_GETODELAY:
831 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
832 		if (error) {
833 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
834 			    __func__, error));
835 			goto out;
836 		}
837 		idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
838 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
839 		if (error) {
840 			DPRINTF(("%s: SNDCTL_DSP_GETODELAY %d\n",
841 			    __func__, error));
842 			goto out;
843 		}
844 		break;
845 	case OSS_SNDCTL_DSP_PROFILE:
846 		/* This gives just a hint to the driver,
847 		 * implementing it as a NOP is ok
848 		 */
849 		DPRINTF(("%s: SNDCTL_DSP_PROFILE\n", __func__));
850 		break;
851 	default:
852 		DPRINTF(("%s: Unimplemented 0x%lx\n", __func__, com));
853 		error = EINVAL;
854 		goto out;
855 	}
856 
857  out:
858  	fd_putfile(SCARG(uap, fd));
859 	return error;
860 }
861 
862 /* If the NetBSD mixer device should have more than 32 devices
863  * some will not be available to Linux */
864 #define NETBSD_MAXDEVS 64
865 struct audiodevinfo {
866 	int done;
867 	dev_t dev;
868 	int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
869 	        rdevmap[NETBSD_MAXDEVS];
870 	char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
871 	int enum2opaque[NETBSD_MAXDEVS];
872         u_long devmask, recmask, stereomask;
873 	u_long caps, source;
874 };
875 
876 static int
877 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
878 {
879 	int i, o;
880 
881 	for (i = 0; i < NETBSD_MAXDEVS; i++) {
882 		o = di->enum2opaque[i];
883 		if (o == opq)
884 			break;
885 		if (o == -1 && label != NULL &&
886 		    !strncmp(di->names[i], label->name, sizeof di->names[i])) {
887 			di->enum2opaque[i] = opq;
888 			break;
889 		}
890 	}
891 	if (i >= NETBSD_MAXDEVS)
892 		i = -1;
893 	/*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
894 	return (i);
895 }
896 
897 static int
898 enum_to_ord(struct audiodevinfo *di, int enm)
899 {
900 	if (enm >= NETBSD_MAXDEVS)
901 		return (-1);
902 
903 	/*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
904 	return (di->enum2opaque[enm]);
905 }
906 
907 static int
908 enum_to_mask(struct audiodevinfo *di, int enm)
909 {
910 	int m;
911 	if (enm >= NETBSD_MAXDEVS)
912 		return (0);
913 
914 	m = di->enum2opaque[enm];
915 	if (m == -1)
916 		m = 0;
917 	/*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
918 	return (m);
919 }
920 
921 /*
922  * Collect the audio device information to allow faster
923  * emulation of the Linux mixer ioctls.  Cache the information
924  * to eliminate the overhead of repeating all the ioctls needed
925  * to collect the information.
926  */
927 static struct audiodevinfo *
928 getdevinfo(file_t *fp)
929 {
930 	mixer_devinfo_t mi;
931 	int i, j, e;
932 	static const struct {
933 		const char *name;
934 		int code;
935 	} *dp, devs[] = {
936 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
937 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
938 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
939 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
940 		{ AudioNaux,		OSS_SOUND_MIXER_LINE1 },
941 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
942 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
943 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
944 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
945 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
946 /*		{ AudioNheadphone,	?? },*/
947 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
948 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
949 /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
950 /*		{ AudioNstereo,		?? },*/
951 /*		{ AudioNmono,		?? },*/
952 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
953 /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
954 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
955 /*		{ AudioNmixerout,	?? },*/
956 		{ 0, -1 }
957 	};
958 	int (*ioctlf)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
959 	struct vnode *vp;
960 	struct vattr va;
961 	static struct audiodevinfo devcache;
962 	struct audiodevinfo *di = &devcache;
963 	int error, mlen, dlen;
964 
965 	/*
966 	 * Figure out what device it is so we can check if the
967 	 * cached data is valid.
968 	 */
969 	vp = fp->f_vnode;
970 	if (vp->v_type != VCHR)
971 		return 0;
972 	vn_lock(vp, LK_SHARED | LK_RETRY);
973 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
974 	VOP_UNLOCK(vp);
975 	if (error)
976 		return 0;
977 	if (di->done && di->dev == va.va_rdev)
978 		return di;
979 
980 	di->done = 1;
981 	di->dev = va.va_rdev;
982 	di->devmask = 0;
983 	di->recmask = 0;
984 	di->stereomask = 0;
985 	di->source = ~0;
986 	di->caps = 0;
987 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
988 		di->devmap[i] = -1;
989 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
990 		di->rdevmap[i] = -1;
991 		di->names[i][0] = '\0';
992 		di->enum2opaque[i] = -1;
993 	}
994 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
995 		mi.index = i;
996 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) != 0)
997 			break;
998 		switch(mi.type) {
999 		case AUDIO_MIXER_VALUE:
1000 			for(dp = devs; dp->name; dp++) {
1001 				if (strcmp(dp->name, mi.label.name) == 0)
1002 					break;
1003 				dlen = strlen(dp->name);
1004 				mlen = strlen(mi.label.name);
1005 				if (dlen < mlen
1006 				    && mi.label.name[mlen-dlen-1] == '.'
1007 				    && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
1008 					break;
1009 			}
1010 			if (dp->code >= 0) {
1011 				di->devmap[dp->code] = i;
1012 				di->rdevmap[i] = dp->code;
1013 				di->devmask |= 1 << dp->code;
1014 				if (mi.un.v.num_channels == 2)
1015 					di->stereomask |= 1 << dp->code;
1016 				strncpy(di->names[i], mi.label.name,
1017 					sizeof di->names[i]);
1018 			}
1019 			break;
1020 		}
1021 	}
1022 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
1023 		mi.index = i;
1024 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) != 0)
1025 			break;
1026 		if (strcmp(mi.label.name, AudioNsource) != 0)
1027 			continue;
1028 		di->source = i;
1029 		switch(mi.type) {
1030 		case AUDIO_MIXER_ENUM:
1031 			for(j = 0; j < mi.un.e.num_mem; j++) {
1032 				e = opaque_to_enum(di,
1033 						   &mi.un.e.member[j].label,
1034 						   mi.un.e.member[j].ord);
1035 				if (e >= 0)
1036 					di->recmask |= 1 << di->rdevmap[e];
1037 			}
1038 			di->caps = OSS_SOUND_CAP_EXCL_INPUT;
1039 			break;
1040 		case AUDIO_MIXER_SET:
1041 			for(j = 0; j < mi.un.s.num_mem; j++) {
1042 				e = opaque_to_enum(di,
1043 						   &mi.un.s.member[j].label,
1044 						   mi.un.s.member[j].mask);
1045 				if (e >= 0)
1046 					di->recmask |= 1 << di->rdevmap[e];
1047 			}
1048 			break;
1049 		}
1050 	}
1051 	return di;
1052 }
1053 
1054 int
1055 oss_ioctl_mixer(struct lwp *lwp, const struct oss_sys_ioctl_args *uap, register_t *retval)
1056 {
1057 	/* {
1058 		syscallarg(int) fd;
1059 		syscallarg(u_long) com;
1060 		syscallarg(void *) data;
1061 	} */
1062 	file_t *fp;
1063 	u_long com;
1064 	struct audiodevinfo *di;
1065 	mixer_ctrl_t mc;
1066 	struct oss_mixer_info omi;
1067 	struct audio_device adev;
1068 	int idat;
1069 	int i;
1070 	int error;
1071 	int l, r, n, e;
1072 	int (*ioctlf)(file_t *, u_long, void *);
1073 
1074 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1075 		return error;
1076 
1077 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1078 		error = EBADF;
1079 		goto out;
1080 	}
1081 
1082 	com = SCARG(uap, com);
1083 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1084 
1085 	retval[0] = 0;
1086 
1087 	di = getdevinfo(fp);
1088 	if (di == 0) {
1089 		error = EINVAL;
1090 		goto out;
1091 	}
1092 
1093 	ioctlf = fp->f_ops->fo_ioctl;
1094 	switch (com) {
1095 	case OSS_GET_VERSION:
1096 		idat = OSS_SOUND_VERSION;
1097 		break;
1098 	case OSS_SOUND_MIXER_INFO:
1099 	case OSS_SOUND_OLD_MIXER_INFO:
1100 		error = ioctlf(fp, AUDIO_GETDEV, &adev);
1101 		if (error) {
1102 			DPRINTF(("%s: AUDIO_GETDEV %d\n",
1103 			    __func__, error));
1104 			goto out;
1105 		}
1106 		memset(&omi, 0, sizeof omi);
1107 		omi.modify_counter = 1;
1108 		strncpy(omi.id, adev.name, sizeof omi.id);
1109 		strncpy(omi.name, adev.name, sizeof omi.name);
1110 		error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
1111 		if (error) {
1112 			DPRINTF(("%s: OSS_SOUND_MIXER_INFO %d\n",
1113 			    __func__, error));
1114 			goto out;
1115 		}
1116 		break;
1117 	case OSS_SOUND_MIXER_READ_RECSRC:
1118 		if (di->source == (u_long)-1) {
1119 			DPRINTF(("%s: OSS_SOUND_MIXER_READ_RECSRC bad source\n",
1120 			    __func__));
1121 			error = EINVAL;
1122 			goto out;
1123 		}
1124 		mc.dev = di->source;
1125 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1126 			mc.type = AUDIO_MIXER_ENUM;
1127 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1128 			if (error) {
1129 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1130 				    __func__, error));
1131 				goto out;
1132 			}
1133 			e = opaque_to_enum(di, NULL, mc.un.ord);
1134 			if (e >= 0)
1135 				idat = 1 << di->rdevmap[e];
1136 		} else {
1137 			mc.type = AUDIO_MIXER_SET;
1138 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1139 			if (error) {
1140 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1141 				    __func__, error));
1142 				goto out;
1143 			}
1144 			e = opaque_to_enum(di, NULL, mc.un.mask);
1145 			if (e >= 0)
1146 				idat = 1 << di->rdevmap[e];
1147 		}
1148 		break;
1149 	case OSS_SOUND_MIXER_READ_DEVMASK:
1150 		idat = di->devmask;
1151 		break;
1152 	case OSS_SOUND_MIXER_READ_RECMASK:
1153 		idat = di->recmask;
1154 		break;
1155 	case OSS_SOUND_MIXER_READ_STEREODEVS:
1156 		idat = di->stereomask;
1157 		break;
1158 	case OSS_SOUND_MIXER_READ_CAPS:
1159 		idat = di->caps;
1160 		break;
1161 	case OSS_SOUND_MIXER_WRITE_RECSRC:
1162 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
1163 		if (di->source == (u_long)-1) {
1164 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC bad "
1165 			    "source\n", __func__));
1166 			error = EINVAL;
1167 			goto out;
1168 		}
1169 		mc.dev = di->source;
1170 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1171 		if (error) {
1172 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC %d\n",
1173 			    __func__, error));
1174 			goto out;
1175 		}
1176 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1177 			mc.type = AUDIO_MIXER_ENUM;
1178 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
1179 				if (idat & (1 << i))
1180 					break;
1181 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
1182 			    di->devmap[i] == -1) {
1183 				error = EINVAL;
1184 				DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC "
1185 				    "bad index %d\n", __func__, i));
1186 				goto out;
1187 			}
1188 			mc.un.ord = enum_to_ord(di, di->devmap[i]);
1189 		} else {
1190 			mc.type = AUDIO_MIXER_SET;
1191 			mc.un.mask = 0;
1192 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
1193 				if (idat & (1 << i)) {
1194 					if (di->devmap[i] == -1) {
1195 						DPRINTF(("%s: OSS_SOUND_MIXER_"
1196 						    "WRITE_RECSRC bad devmap "
1197 						    "%d\n", __func__, i));
1198 						error = EINVAL;
1199 						goto out;
1200 					}
1201 					mc.un.mask |= enum_to_mask(di,
1202 					    di->devmap[i]);
1203 				}
1204 			}
1205 		}
1206 		error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1207 		if (error) {
1208 			DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1209 			    __func__, error));
1210 			goto out;
1211 		}
1212 		goto out;
1213 	default:
1214 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
1215 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
1216 			n = OSS_GET_DEV(com);
1217 			if (di->devmap[n] == -1) {
1218 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
1219 				    __func__, com, n));
1220 				error = EINVAL;
1221 				goto out;
1222 			}
1223 		    doread:
1224 			mc.dev = di->devmap[n];
1225 			mc.type = AUDIO_MIXER_VALUE;
1226 			mc.un.value.num_channels = di->stereomask &
1227 			    (1 << n) ? 2 : 1;
1228 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1229 			if (error) {
1230 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1231 				    __func__, error));
1232 				goto out;
1233 			}
1234 			if (mc.un.value.num_channels != 2) {
1235 				l = r =
1236 				    mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
1237 			} else {
1238 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1239 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1240 			}
1241 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
1242 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1243 				 __func__, n, di->devmap[n], l, r, idat));
1244 			break;
1245 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
1246 		    com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
1247 		    (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1248 		    com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
1249 			n = OSS_GET_DEV(com);
1250 			if (di->devmap[n] == -1) {
1251 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
1252 				    __func__, com, n));
1253 				error = EINVAL;
1254 				goto out;
1255 			}
1256 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
1257 			if (error) {
1258 				DPRINTF(("%s: 0x%lx error %d\n",
1259 				    __func__, com, error));
1260 				goto out;
1261 			}
1262 			l = FROM_OSSVOL( idat       & 0xff);
1263 			r = FROM_OSSVOL((idat >> 8) & 0xff);
1264 			mc.dev = di->devmap[n];
1265 			mc.type = AUDIO_MIXER_VALUE;
1266 			if (di->stereomask & (1 << n)) {
1267 				mc.un.value.num_channels = 2;
1268 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1269 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1270 			} else {
1271 				mc.un.value.num_channels = 1;
1272 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1273 				    (l + r) / 2;
1274 			}
1275 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1276 			     __func__, n, di->devmap[n], l, r, idat));
1277 			error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1278 			if (error) {
1279 				DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1280 				    __func__, error));
1281 				goto out;
1282 			}
1283 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1284 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
1285 				error = 0;
1286 				goto out;
1287 			}
1288 			goto doread;
1289 		} else {
1290 			DPRINTF(("%s: Unknown mixer ioctl 0x%lx\n", __func__,
1291 			    com));
1292 			error = EINVAL;
1293 			goto out;
1294 		}
1295 	}
1296 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
1297  out:
1298  	fd_putfile(SCARG(uap, fd));
1299 	return error;
1300 }
1301 
1302 /* Sequencer emulation */
1303 int
1304 oss_ioctl_sequencer(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
1305 {
1306 	/* {
1307 		syscallarg(int) fd;
1308 		syscallarg(u_long) com;
1309 		syscallarg(void *) data;
1310 	} */
1311 	file_t *fp;
1312 	u_long com;
1313 	int idat, idat1;
1314 	struct synth_info si;
1315 	struct oss_synth_info osi;
1316 	struct oss_seq_event_rec oser;
1317 	int error;
1318 	int (*ioctlf)(file_t *, u_long, void *);
1319 
1320 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1321 		return (EBADF);
1322 
1323 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1324 		error = EBADF;
1325 		goto out;
1326 	}
1327 
1328 	com = SCARG(uap, com);
1329 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1330 
1331 	retval[0] = 0;
1332 
1333 	ioctlf = fp->f_ops->fo_ioctl;
1334 	switch (com) {
1335 	case OSS_SEQ_RESET:
1336 		error = ioctlf(fp, SEQUENCER_RESET, &idat);
1337 		goto out;
1338 	case OSS_SEQ_SYNC:
1339 		error = ioctlf(fp, SEQUENCER_SYNC, &idat);
1340 		goto out;
1341 	case OSS_SYNTH_INFO:
1342 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
1343 		if (error)
1344 			goto out;
1345 		si.device = osi.device;
1346 		error = ioctlf(fp, SEQUENCER_INFO, &si);
1347 		if (error)
1348 			goto out;
1349 		strncpy(osi.name, si.name, sizeof osi.name);
1350 		osi.device = si.device;
1351 		switch(si.synth_type) {
1352 		case SYNTH_TYPE_FM:
1353 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
1354 		case SYNTH_TYPE_SAMPLE:
1355 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
1356 		case SYNTH_TYPE_MIDI:
1357 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
1358 		default:
1359 			osi.synth_type = 0; break;
1360 		}
1361 		switch(si.synth_subtype) {
1362 		case SYNTH_SUB_FM_TYPE_ADLIB:
1363 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
1364 		case SYNTH_SUB_FM_TYPE_OPL3:
1365 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
1366 		case SYNTH_SUB_MIDI_TYPE_MPU401:
1367 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
1368 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
1369 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
1370 		default:
1371 			osi.synth_subtype = 0; break;
1372 		}
1373 		osi.perc_mode = 0;
1374 		osi.nr_voices = si.nr_voices;
1375 		osi.nr_drums = 0;
1376 		osi.instr_bank_size = si.instr_bank_size;
1377 		osi.capabilities = 0;
1378 		if (si.capabilities & SYNTH_CAP_OPL3)
1379 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
1380 		if (si.capabilities & SYNTH_CAP_INPUT)
1381 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
1382 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
1383 		goto out;
1384 	case OSS_SEQ_CTRLRATE:
1385 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1386 		if (error)
1387 			goto out;
1388 		error = ioctlf(fp, SEQUENCER_CTRLRATE, &idat);
1389 		if (error)
1390 			goto out;
1391 		retval[0] = idat;
1392 		break;
1393 	case OSS_SEQ_GETOUTCOUNT:
1394 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, &idat);
1395 		if (error)
1396 			goto out;
1397 		retval[0] = idat;
1398 		break;
1399 	case OSS_SEQ_GETINCOUNT:
1400 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, &idat);
1401 		if (error)
1402 			goto out;
1403 		retval[0] = idat;
1404 		break;
1405 	case OSS_SEQ_NRSYNTHS:
1406 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, &idat);
1407 		if (error)
1408 			goto out;
1409 		retval[0] = idat;
1410 		break;
1411 	case OSS_SEQ_NRMIDIS:
1412 		error = ioctlf(fp, SEQUENCER_NRMIDIS, &idat);
1413 		if (error)
1414 			goto out;
1415 		retval[0] = idat;
1416 		break;
1417 	case OSS_SEQ_THRESHOLD:
1418 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1419 		if (error)
1420 			goto out;
1421 		error = ioctlf(fp, SEQUENCER_THRESHOLD, &idat);
1422 		goto out;
1423 	case OSS_MEMAVL:
1424 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1425 		if (error)
1426 			goto out;
1427 		error = ioctlf(fp, SEQUENCER_MEMAVL, &idat);
1428 		if (error)
1429 			goto out;
1430 		retval[0] = idat;
1431 		break;
1432 	case OSS_SEQ_PANIC:
1433 		error = ioctlf(fp, SEQUENCER_PANIC, &idat);
1434 		goto out;
1435 	case OSS_SEQ_OUTOFBAND:
1436 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
1437 		if (error)
1438 			goto out;
1439 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, &oser);
1440 		if (error)
1441 			goto out;
1442 		break;
1443 	case OSS_SEQ_GETTIME:
1444 		error = ioctlf(fp, SEQUENCER_GETTIME, &idat);
1445 		if (error)
1446 			goto out;
1447 		retval[0] = idat;
1448 		break;
1449 	case OSS_TMR_TIMEBASE:
1450 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1451 		if (error)
1452 			goto out;
1453 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, &idat);
1454 		if (error)
1455 			goto out;
1456 		retval[0] = idat;
1457 		break;
1458 	case OSS_TMR_START:
1459 		error = ioctlf(fp, SEQUENCER_TMR_START, &idat);
1460 		goto out;
1461 	case OSS_TMR_STOP:
1462 		error = ioctlf(fp, SEQUENCER_TMR_STOP, &idat);
1463 		goto out;
1464 	case OSS_TMR_CONTINUE:
1465 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, &idat);
1466 		goto out;
1467 	case OSS_TMR_TEMPO:
1468 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1469 		if (error)
1470 			goto out;
1471 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, &idat);
1472 		if (error)
1473 			goto out;
1474 		retval[0] = idat;
1475 		break;
1476 	case OSS_TMR_SOURCE:
1477 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
1478 		if (error)
1479 			goto out;
1480 		idat = 0;
1481 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
1482 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, &idat);
1483 		if (error)
1484 			goto out;
1485 		idat1 = idat;
1486 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
1487 		retval[0] = idat;
1488 		break;
1489 	case OSS_TMR_METRONOME:
1490 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1491 		if (error)
1492 			goto out;
1493 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, &idat);
1494 		goto out;
1495 	case OSS_TMR_SELECT:
1496 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1497 		if (error)
1498 			goto out;
1499 		retval[0] = idat;
1500 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, &idat);
1501 		goto out;
1502 	default:
1503 		DPRINTF(("%s: Unknown sequencer command 0x%lx\n", __func__,
1504 		    com));
1505 		error = EINVAL;
1506 		goto out;
1507 	}
1508 
1509 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
1510  out:
1511 	fd_putfile(SCARG(uap, fd));
1512 	return error;
1513 }
1514 
1515 /*
1516  * When AUDIO_SETINFO fails to set a channel count, the application's chosen
1517  * number is out of range of what the kernel allows.
1518  *
1519  * When this happens, we use the current hardware settings. This is just in
1520  * case an application is abusing SNDCTL_DSP_CHANNELS - OSSv4 always sets and
1521  * returns a reasonable value, even if it wasn't what the user requested.
1522  *
1523  * XXX: If a device is opened for both playback and recording, and supports
1524  * fewer channels for recording than playback, applications that do both will
1525  * behave very strangely. OSS doesn't allow for reporting separate channel
1526  * counts for recording and playback. This could be worked around by always
1527  * mixing recorded data up to the same number of channels as is being used
1528  * for playback.
1529  */
1530 static void
1531 setchannels(file_t *fp, int mode, int nchannels)
1532 {
1533 	struct audio_info tmpinfo, hwfmt;
1534 	int (*ioctlf)(file_t *, u_long, void *);
1535 
1536 	ioctlf = fp->f_ops->fo_ioctl;
1537 
1538 	if (ioctlf(fp, AUDIO_GETFORMAT, &hwfmt) < 0) {
1539 		hwfmt.record.channels = hwfmt.play.channels = 2;
1540 	}
1541 
1542 	if (mode & AUMODE_PLAY) {
1543 		AUDIO_INITINFO(&tmpinfo);
1544 		tmpinfo.play.channels = nchannels;
1545 		if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) < 0) {
1546 			AUDIO_INITINFO(&tmpinfo);
1547 			tmpinfo.play.channels = hwfmt.play.channels;
1548 			(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
1549 		}
1550 	}
1551 
1552 	if (mode & AUMODE_RECORD) {
1553 		AUDIO_INITINFO(&tmpinfo);
1554 		tmpinfo.record.channels = nchannels;
1555 		if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) < 0) {
1556 			AUDIO_INITINFO(&tmpinfo);
1557 			tmpinfo.record.channels = hwfmt.record.channels;
1558 			(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
1559 		}
1560 	}
1561 }
1562 
1563 /*
1564  * Check that the blocksize is a power of 2 as OSS wants.
1565  * If not, set it to be.
1566  */
1567 static void
1568 setblocksize(file_t *fp, struct audio_info *info)
1569 {
1570 	struct audio_info set;
1571 	u_int s;
1572 
1573 	 if (info->blocksize & (info->blocksize - 1)) {
1574 		for(s = 32; s < info->blocksize; s <<= 1)
1575 			continue;
1576 		AUDIO_INITINFO(&set);
1577 		set.blocksize = s;
1578 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, &set);
1579 		fp->f_ops->fo_ioctl(fp, AUDIO_GETBUFINFO, info);
1580 	}
1581 }
1582