xref: /netbsd-src/sys/compat/ossaudio/ossaudio.c (revision 46aa4c99e5d4c222a3ca73d6db7f1efb2c1db9b6)
1 /*	$NetBSD: ossaudio.c,v 1.85 2023/06/20 15:22:04 riastradh 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.85 2023/06/20 15:22:04 riastradh 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 *
compat_ossaudio_getcmd(u_long cmd)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
compat_ossaudio_modcmd(modcmd_t cmd,void * arg)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
oss_ioctl_audio(struct lwp * l,const struct oss_sys_ioctl_args * uap,register_t * retval)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 		memset(&bufinfo, 0, sizeof(bufinfo));
668 		bufinfo.fragsize = tmpinfo.blocksize;
669 		bufinfo.fragments = tmpinfo.hiwat -
670 		    (tmpinfo.play.seek + tmpinfo.blocksize - 1) /
671 		    tmpinfo.blocksize;
672 		bufinfo.fragstotal = tmpinfo.hiwat;
673 		bufinfo.bytes =
674 		    tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
675 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
676 		if (error) {
677 			DPRINTF(("%s: SNDCTL_DSP_GETOSPACE = %d\n",
678 			    __func__, error));
679 			goto out;
680 		}
681 		break;
682 	case OSS_SNDCTL_DSP_GETISPACE:
683 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
684 		if (error) {
685 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
686 			     __func__, error));
687 			goto out;
688 		}
689 		setblocksize(fp, &tmpinfo);
690 		memset(&bufinfo, 0, sizeof(bufinfo));
691 		bufinfo.fragsize = tmpinfo.blocksize;
692 		bufinfo.fragments = tmpinfo.record.seek / tmpinfo.blocksize;
693 		bufinfo.fragstotal =
694 		    tmpinfo.record.buffer_size / tmpinfo.blocksize;
695 		bufinfo.bytes = tmpinfo.record.seek;
696 		error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
697 		if (error) {
698 			DPRINTF(("%s: SNDCTL_DSP_GETISPACE %d %d %d %d = %d\n",
699 			     __func__, bufinfo.fragsize, bufinfo.fragments,
700 			     bufinfo.fragstotal, bufinfo.bytes, error));
701 			goto out;
702 		}
703 		break;
704 	case OSS_SNDCTL_DSP_NONBLOCK:
705 		idat = 1;
706 		error = ioctlf(fp, FIONBIO, &idat);
707 		if (error) {
708 			DPRINTF(("%s: FIONBIO %d\n",
709 			     __func__, error));
710 			goto out;
711 		}
712 		break;
713 	case OSS_SNDCTL_DSP_GETCAPS:
714 		error = ioctlf(fp, AUDIO_GETPROPS, &idata);
715 		if (error) {
716 			DPRINTF(("%s: AUDIO_GETPROPS %d\n",
717 			     __func__, error));
718 			goto out;
719 		}
720 		idat = OSS_DSP_CAP_TRIGGER;
721 		if (idata & AUDIO_PROP_FULLDUPLEX)
722 			idat |= OSS_DSP_CAP_DUPLEX;
723 		if (idata & AUDIO_PROP_MMAP)
724 			idat |= OSS_DSP_CAP_MMAP;
725 		DPRINTF(("%s: SNDCL_DSP_GETCAPS %s duplex, %smmap\n",
726 		     __func__, (idat & OSS_DSP_CAP_DUPLEX) ? "full" : "half",
727 		     (idat & OSS_DSP_CAP_MMAP) ? "" : "no "));
728 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
729 		if (error) {
730 			DPRINTF(("%s: SNDCTL_DSP_GETCAPS %x = %d\n", __func__,
731 			    idat, error));
732 			goto out;
733 		}
734 		break;
735 	case OSS_SNDCTL_DSP_SETTRIGGER:
736 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
737 		if (error) {
738 			DPRINTF(("%s: SNDCTL_DSP_SETTRIGGER: %d\n",
739 			     __func__, error));
740 			goto out;
741 		}
742 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
743 		if (error) {
744 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
745 			     __func__, error));
746 			goto out;
747 		}
748 		AUDIO_INITINFO(&tmpinfo);
749 		if (tmpinfo.mode & AUMODE_PLAY)
750 			tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
751 		if (tmpinfo.mode & AUMODE_RECORD)
752 			tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
753 		(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
754 		/* FALLTHRU */
755 	case OSS_SNDCTL_DSP_GETTRIGGER:
756 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
757 		if (error) {
758 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
759 			     __func__, error));
760 			goto out;
761 		}
762 		idat = 0;
763 		if ((tmpinfo.mode & AUMODE_PLAY) && !tmpinfo.play.pause)
764 			idat |= OSS_PCM_ENABLE_OUTPUT;
765 		if ((tmpinfo.mode & AUMODE_RECORD) && !tmpinfo.record.pause)
766 			idat |= OSS_PCM_ENABLE_INPUT;
767 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
768 		if (error) {
769 			DPRINTF(("%s: SNDCTL_DSP_GETTRIGGER = %x = %d\n",
770 			    __func__, idat, error));
771 			goto out;
772 		}
773 		break;
774 	case OSS_SNDCTL_DSP_GETIPTR:
775 		error = ioctlf(fp, AUDIO_GETIOFFS, &tmpoffs);
776 		if (error) {
777 			DPRINTF(("%s: AUDIO_GETIOFFS %d\n",
778 			     __func__, error));
779 			goto out;
780 		}
781 		memset(&cntinfo, 0, sizeof(cntinfo));
782 		cntinfo.bytes = tmpoffs.samples;
783 		cntinfo.blocks = tmpoffs.deltablks;
784 		cntinfo.ptr = tmpoffs.offset;
785 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
786 		if (error) {
787 			DPRINTF(("%s: SNDCTL_DSP_GETIPTR %d\n",
788 			    __func__, error));
789 			goto out;
790 		}
791 		break;
792 	case OSS_SNDCTL_DSP_GETOPTR:
793 		error = ioctlf(fp, AUDIO_GETOOFFS, &tmpoffs);
794 		if (error) {
795 			DPRINTF(("%s: AUDIO_GETOOFFS %d\n",
796 			     __func__, error));
797 			goto out;
798 		}
799 		memset(&cntinfo, 0, sizeof(cntinfo));
800 		cntinfo.bytes = tmpoffs.samples;
801 		cntinfo.blocks = tmpoffs.deltablks;
802 		cntinfo.ptr = tmpoffs.offset;
803 		error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
804 		if (error) {
805 			DPRINTF(("%s: SNDCTL_DSP_GETOPTR %d\n",
806 			    __func__, error));
807 			goto out;
808 		}
809 		break;
810 	case OSS_SNDCTL_DSP_SETDUPLEX:
811 		idat = 1;
812 		error = ioctlf(fp, AUDIO_SETFD, &idat);
813 		if (error) {
814 			DPRINTF(("%s: AUDIO_SETFD %d = %d\n",
815 			    __func__, idat, error));
816 			goto out;
817 		}
818 		break;
819 	case OSS_SNDCTL_DSP_MAPINBUF:
820 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPINBUF\n",
821 		    __func__));
822 		error = EINVAL;
823 		goto out;
824 	case OSS_SNDCTL_DSP_MAPOUTBUF:
825 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPOUTBUF\n",
826 		    __func__));
827 		error = EINVAL;
828 		goto out;
829 	case OSS_SNDCTL_DSP_SETSYNCRO:
830 		DPRINTF(("%s: Unimplemented SNDCTL_DSP_GETSYNCHRO\n",
831 		    __func__));
832 		error = EINVAL;
833 		goto out;
834 	case OSS_SNDCTL_DSP_GETODELAY:
835 		error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
836 		if (error) {
837 			DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
838 			    __func__, error));
839 			goto out;
840 		}
841 		idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
842 		error = copyout(&idat, SCARG(uap, data), sizeof idat);
843 		if (error) {
844 			DPRINTF(("%s: SNDCTL_DSP_GETODELAY %d\n",
845 			    __func__, error));
846 			goto out;
847 		}
848 		break;
849 	case OSS_SNDCTL_DSP_PROFILE:
850 		/* This gives just a hint to the driver,
851 		 * implementing it as a NOP is ok
852 		 */
853 		DPRINTF(("%s: SNDCTL_DSP_PROFILE\n", __func__));
854 		break;
855 	default:
856 		DPRINTF(("%s: Unimplemented 0x%lx\n", __func__, com));
857 		error = EINVAL;
858 		goto out;
859 	}
860 
861  out:
862  	fd_putfile(SCARG(uap, fd));
863 	return error;
864 }
865 
866 /* If the NetBSD mixer device should have more than 32 devices
867  * some will not be available to Linux */
868 #define NETBSD_MAXDEVS 64
869 struct audiodevinfo {
870 	int done;
871 	dev_t dev;
872 	int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
873 	        rdevmap[NETBSD_MAXDEVS];
874 	char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
875 	int enum2opaque[NETBSD_MAXDEVS];
876         u_long devmask, recmask, stereomask;
877 	u_long caps, source;
878 };
879 
880 static int
opaque_to_enum(struct audiodevinfo * di,audio_mixer_name_t * label,int opq)881 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
882 {
883 	int i, o;
884 
885 	for (i = 0; i < NETBSD_MAXDEVS; i++) {
886 		o = di->enum2opaque[i];
887 		if (o == opq)
888 			break;
889 		if (o == -1 && label != NULL &&
890 		    !strncmp(di->names[i], label->name, sizeof di->names[i])) {
891 			di->enum2opaque[i] = opq;
892 			break;
893 		}
894 	}
895 	if (i >= NETBSD_MAXDEVS)
896 		i = -1;
897 	/*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
898 	return (i);
899 }
900 
901 static int
enum_to_ord(struct audiodevinfo * di,int enm)902 enum_to_ord(struct audiodevinfo *di, int enm)
903 {
904 	if (enm >= NETBSD_MAXDEVS)
905 		return (-1);
906 
907 	/*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
908 	return (di->enum2opaque[enm]);
909 }
910 
911 static int
enum_to_mask(struct audiodevinfo * di,int enm)912 enum_to_mask(struct audiodevinfo *di, int enm)
913 {
914 	int m;
915 	if (enm >= NETBSD_MAXDEVS)
916 		return (0);
917 
918 	m = di->enum2opaque[enm];
919 	if (m == -1)
920 		m = 0;
921 	/*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
922 	return (m);
923 }
924 
925 /*
926  * Collect the audio device information to allow faster
927  * emulation of the Linux mixer ioctls.  Cache the information
928  * to eliminate the overhead of repeating all the ioctls needed
929  * to collect the information.
930  */
931 static struct audiodevinfo *
getdevinfo(file_t * fp)932 getdevinfo(file_t *fp)
933 {
934 	mixer_devinfo_t mi;
935 	int i, j, e;
936 	static const struct {
937 		const char *name;
938 		int code;
939 	} *dp, devs[] = {
940 		{ AudioNmicrophone,	OSS_SOUND_MIXER_MIC },
941 		{ AudioNline,		OSS_SOUND_MIXER_LINE },
942 		{ AudioNcd,		OSS_SOUND_MIXER_CD },
943 		{ AudioNdac,		OSS_SOUND_MIXER_PCM },
944 		{ AudioNaux,		OSS_SOUND_MIXER_LINE1 },
945 		{ AudioNrecord,		OSS_SOUND_MIXER_IMIX },
946 		{ AudioNmaster,		OSS_SOUND_MIXER_VOLUME },
947 		{ AudioNtreble,		OSS_SOUND_MIXER_TREBLE },
948 		{ AudioNbass,		OSS_SOUND_MIXER_BASS },
949 		{ AudioNspeaker,	OSS_SOUND_MIXER_SPEAKER },
950 /*		{ AudioNheadphone,	?? },*/
951 		{ AudioNoutput,		OSS_SOUND_MIXER_OGAIN },
952 		{ AudioNinput,		OSS_SOUND_MIXER_IGAIN },
953 /*		{ AudioNmaster,		OSS_SOUND_MIXER_SPEAKER },*/
954 /*		{ AudioNstereo,		?? },*/
955 /*		{ AudioNmono,		?? },*/
956 		{ AudioNfmsynth,	OSS_SOUND_MIXER_SYNTH },
957 /*		{ AudioNwave,		OSS_SOUND_MIXER_PCM },*/
958 		{ AudioNmidi,		OSS_SOUND_MIXER_SYNTH },
959 /*		{ AudioNmixerout,	?? },*/
960 		{ 0, -1 }
961 	};
962 	int (*ioctlf)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
963 	struct vnode *vp;
964 	struct vattr va;
965 	static struct audiodevinfo devcache;
966 	struct audiodevinfo *di = &devcache;
967 	int error, mlen, dlen;
968 
969 	/*
970 	 * Figure out what device it is so we can check if the
971 	 * cached data is valid.
972 	 */
973 	vp = fp->f_vnode;
974 	if (vp->v_type != VCHR)
975 		return 0;
976 	vn_lock(vp, LK_SHARED | LK_RETRY);
977 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
978 	VOP_UNLOCK(vp);
979 	if (error)
980 		return 0;
981 	if (di->done && di->dev == va.va_rdev)
982 		return di;
983 
984 	di->done = 1;
985 	di->dev = va.va_rdev;
986 	di->devmask = 0;
987 	di->recmask = 0;
988 	di->stereomask = 0;
989 	di->source = ~0;
990 	di->caps = 0;
991 	for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
992 		di->devmap[i] = -1;
993 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
994 		di->rdevmap[i] = -1;
995 		di->names[i][0] = '\0';
996 		di->enum2opaque[i] = -1;
997 	}
998 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
999 		mi.index = i;
1000 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) != 0)
1001 			break;
1002 		switch(mi.type) {
1003 		case AUDIO_MIXER_VALUE:
1004 			for(dp = devs; dp->name; dp++) {
1005 				if (strcmp(dp->name, mi.label.name) == 0)
1006 					break;
1007 				dlen = strlen(dp->name);
1008 				mlen = strlen(mi.label.name);
1009 				if (dlen < mlen
1010 				    && mi.label.name[mlen-dlen-1] == '.'
1011 				    && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
1012 					break;
1013 			}
1014 			if (dp->code >= 0) {
1015 				di->devmap[dp->code] = i;
1016 				di->rdevmap[i] = dp->code;
1017 				di->devmask |= 1 << dp->code;
1018 				if (mi.un.v.num_channels == 2)
1019 					di->stereomask |= 1 << dp->code;
1020 				strncpy(di->names[i], mi.label.name,
1021 					sizeof di->names[i]);
1022 			}
1023 			break;
1024 		}
1025 	}
1026 	for(i = 0; i < NETBSD_MAXDEVS; i++) {
1027 		mi.index = i;
1028 		if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) != 0)
1029 			break;
1030 		if (strcmp(mi.label.name, AudioNsource) != 0)
1031 			continue;
1032 		di->source = i;
1033 		switch(mi.type) {
1034 		case AUDIO_MIXER_ENUM:
1035 			for(j = 0; j < mi.un.e.num_mem; j++) {
1036 				e = opaque_to_enum(di,
1037 						   &mi.un.e.member[j].label,
1038 						   mi.un.e.member[j].ord);
1039 				if (e >= 0)
1040 					di->recmask |= 1 << di->rdevmap[e];
1041 			}
1042 			di->caps = OSS_SOUND_CAP_EXCL_INPUT;
1043 			break;
1044 		case AUDIO_MIXER_SET:
1045 			for(j = 0; j < mi.un.s.num_mem; j++) {
1046 				e = opaque_to_enum(di,
1047 						   &mi.un.s.member[j].label,
1048 						   mi.un.s.member[j].mask);
1049 				if (e >= 0)
1050 					di->recmask |= 1 << di->rdevmap[e];
1051 			}
1052 			break;
1053 		}
1054 	}
1055 	return di;
1056 }
1057 
1058 int
oss_ioctl_mixer(struct lwp * lwp,const struct oss_sys_ioctl_args * uap,register_t * retval)1059 oss_ioctl_mixer(struct lwp *lwp, const struct oss_sys_ioctl_args *uap, register_t *retval)
1060 {
1061 	/* {
1062 		syscallarg(int) fd;
1063 		syscallarg(u_long) com;
1064 		syscallarg(void *) data;
1065 	} */
1066 	file_t *fp;
1067 	u_long com;
1068 	struct audiodevinfo *di;
1069 	mixer_ctrl_t mc;
1070 	struct oss_mixer_info omi;
1071 	struct audio_device adev;
1072 	int idat = 0;
1073 	int i;
1074 	int error;
1075 	int l, r, n, e;
1076 	int (*ioctlf)(file_t *, u_long, void *);
1077 
1078 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
1079 		return error;
1080 
1081 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1082 		error = EBADF;
1083 		goto out;
1084 	}
1085 
1086 	com = SCARG(uap, com);
1087 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1088 
1089 	retval[0] = 0;
1090 
1091 	di = getdevinfo(fp);
1092 	if (di == 0) {
1093 		error = EINVAL;
1094 		goto out;
1095 	}
1096 
1097 	ioctlf = fp->f_ops->fo_ioctl;
1098 	switch (com) {
1099 	case OSS_GET_VERSION:
1100 		idat = OSS_SOUND_VERSION;
1101 		break;
1102 	case OSS_SOUND_MIXER_INFO:
1103 	case OSS_SOUND_OLD_MIXER_INFO:
1104 		error = ioctlf(fp, AUDIO_GETDEV, &adev);
1105 		if (error) {
1106 			DPRINTF(("%s: AUDIO_GETDEV %d\n",
1107 			    __func__, error));
1108 			goto out;
1109 		}
1110 		memset(&omi, 0, sizeof omi);
1111 		omi.modify_counter = 1;
1112 		strncpy(omi.id, adev.name, sizeof omi.id);
1113 		strncpy(omi.name, adev.name, sizeof omi.name);
1114 		error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
1115 		if (error) {
1116 			DPRINTF(("%s: OSS_SOUND_MIXER_INFO %d\n",
1117 			    __func__, error));
1118 			goto out;
1119 		}
1120 		break;
1121 	case OSS_SOUND_MIXER_READ_RECSRC:
1122 		if (di->source == (u_long)-1) {
1123 			DPRINTF(("%s: OSS_SOUND_MIXER_READ_RECSRC bad source\n",
1124 			    __func__));
1125 			error = EINVAL;
1126 			goto out;
1127 		}
1128 		mc.dev = di->source;
1129 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1130 			mc.type = AUDIO_MIXER_ENUM;
1131 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1132 			if (error) {
1133 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1134 				    __func__, error));
1135 				goto out;
1136 			}
1137 			e = opaque_to_enum(di, NULL, mc.un.ord);
1138 			if (e >= 0)
1139 				idat = 1 << di->rdevmap[e];
1140 		} else {
1141 			mc.type = AUDIO_MIXER_SET;
1142 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1143 			if (error) {
1144 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1145 				    __func__, error));
1146 				goto out;
1147 			}
1148 			e = opaque_to_enum(di, NULL, mc.un.mask);
1149 			if (e >= 0)
1150 				idat = 1 << di->rdevmap[e];
1151 		}
1152 		break;
1153 	case OSS_SOUND_MIXER_READ_DEVMASK:
1154 		idat = di->devmask;
1155 		break;
1156 	case OSS_SOUND_MIXER_READ_RECMASK:
1157 		idat = di->recmask;
1158 		break;
1159 	case OSS_SOUND_MIXER_READ_STEREODEVS:
1160 		idat = di->stereomask;
1161 		break;
1162 	case OSS_SOUND_MIXER_READ_CAPS:
1163 		idat = di->caps;
1164 		break;
1165 	case OSS_SOUND_MIXER_WRITE_RECSRC:
1166 	case OSS_SOUND_MIXER_WRITE_R_RECSRC:
1167 		if (di->source == (u_long)-1) {
1168 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC bad "
1169 			    "source\n", __func__));
1170 			error = EINVAL;
1171 			goto out;
1172 		}
1173 		mc.dev = di->source;
1174 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1175 		if (error) {
1176 			DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC %d\n",
1177 			    __func__, error));
1178 			goto out;
1179 		}
1180 		if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1181 			mc.type = AUDIO_MIXER_ENUM;
1182 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
1183 				if (idat & (1 << i))
1184 					break;
1185 			if (i >= OSS_SOUND_MIXER_NRDEVICES ||
1186 			    di->devmap[i] == -1) {
1187 				error = EINVAL;
1188 				DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC "
1189 				    "bad index %d\n", __func__, i));
1190 				goto out;
1191 			}
1192 			mc.un.ord = enum_to_ord(di, di->devmap[i]);
1193 		} else {
1194 			mc.type = AUDIO_MIXER_SET;
1195 			mc.un.mask = 0;
1196 			for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
1197 				if (idat & (1 << i)) {
1198 					if (di->devmap[i] == -1) {
1199 						DPRINTF(("%s: OSS_SOUND_MIXER_"
1200 						    "WRITE_RECSRC bad devmap "
1201 						    "%d\n", __func__, i));
1202 						error = EINVAL;
1203 						goto out;
1204 					}
1205 					mc.un.mask |= enum_to_mask(di,
1206 					    di->devmap[i]);
1207 				}
1208 			}
1209 		}
1210 		error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1211 		if (error) {
1212 			DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1213 			    __func__, error));
1214 			goto out;
1215 		}
1216 		goto out;
1217 	default:
1218 		if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
1219 		    com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
1220 			n = OSS_GET_DEV(com);
1221 			if (di->devmap[n] == -1) {
1222 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
1223 				    __func__, com, n));
1224 				error = EINVAL;
1225 				goto out;
1226 			}
1227 		    doread:
1228 			mc.dev = di->devmap[n];
1229 			mc.type = AUDIO_MIXER_VALUE;
1230 			mc.un.value.num_channels = di->stereomask &
1231 			    (1 << n) ? 2 : 1;
1232 			error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1233 			if (error) {
1234 				DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1235 				    __func__, error));
1236 				goto out;
1237 			}
1238 			if (mc.un.value.num_channels != 2) {
1239 				l = r =
1240 				    mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
1241 			} else {
1242 				l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1243 				r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1244 			}
1245 			idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
1246 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1247 				 __func__, n, di->devmap[n], l, r, idat));
1248 			break;
1249 		} else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
1250 		    com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
1251 		    (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1252 		    com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
1253 			n = OSS_GET_DEV(com);
1254 			if (di->devmap[n] == -1) {
1255 				DPRINTF(("%s: 0x%lx bad devmap %d\n",
1256 				    __func__, com, n));
1257 				error = EINVAL;
1258 				goto out;
1259 			}
1260 			error = copyin(SCARG(uap, data), &idat, sizeof idat);
1261 			if (error) {
1262 				DPRINTF(("%s: 0x%lx error %d\n",
1263 				    __func__, com, error));
1264 				goto out;
1265 			}
1266 			l = FROM_OSSVOL( idat       & 0xff);
1267 			r = FROM_OSSVOL((idat >> 8) & 0xff);
1268 			mc.dev = di->devmap[n];
1269 			mc.type = AUDIO_MIXER_VALUE;
1270 			if (di->stereomask & (1 << n)) {
1271 				mc.un.value.num_channels = 2;
1272 				mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1273 				mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1274 			} else {
1275 				mc.un.value.num_channels = 1;
1276 				mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1277 				    (l + r) / 2;
1278 			}
1279 			DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1280 			     __func__, n, di->devmap[n], l, r, idat));
1281 			error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1282 			if (error) {
1283 				DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1284 				    __func__, error));
1285 				goto out;
1286 			}
1287 			if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1288 			   com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
1289 				error = 0;
1290 				goto out;
1291 			}
1292 			goto doread;
1293 		} else {
1294 			DPRINTF(("%s: Unknown mixer ioctl 0x%lx\n", __func__,
1295 			    com));
1296 			error = EINVAL;
1297 			goto out;
1298 		}
1299 	}
1300 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
1301  out:
1302  	fd_putfile(SCARG(uap, fd));
1303 	return error;
1304 }
1305 
1306 /* Sequencer emulation */
1307 int
oss_ioctl_sequencer(struct lwp * l,const struct oss_sys_ioctl_args * uap,register_t * retval)1308 oss_ioctl_sequencer(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
1309 {
1310 	/* {
1311 		syscallarg(int) fd;
1312 		syscallarg(u_long) com;
1313 		syscallarg(void *) data;
1314 	} */
1315 	file_t *fp;
1316 	u_long com;
1317 	int idat, idat1;
1318 	struct synth_info si;
1319 	struct oss_synth_info osi;
1320 	struct oss_seq_event_rec oser;
1321 	int error;
1322 	int (*ioctlf)(file_t *, u_long, void *);
1323 
1324 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1325 		return (EBADF);
1326 
1327 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1328 		error = EBADF;
1329 		goto out;
1330 	}
1331 
1332 	com = SCARG(uap, com);
1333 	DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1334 
1335 	retval[0] = 0;
1336 
1337 	ioctlf = fp->f_ops->fo_ioctl;
1338 	switch (com) {
1339 	case OSS_SEQ_RESET:
1340 		error = ioctlf(fp, SEQUENCER_RESET, &idat);
1341 		goto out;
1342 	case OSS_SEQ_SYNC:
1343 		error = ioctlf(fp, SEQUENCER_SYNC, &idat);
1344 		goto out;
1345 	case OSS_SYNTH_INFO:
1346 		error = copyin(SCARG(uap, data), &osi, sizeof osi);
1347 		if (error)
1348 			goto out;
1349 		si.device = osi.device;
1350 		error = ioctlf(fp, SEQUENCER_INFO, &si);
1351 		if (error)
1352 			goto out;
1353 		strncpy(osi.name, si.name, sizeof osi.name);
1354 		osi.device = si.device;
1355 		switch(si.synth_type) {
1356 		case SYNTH_TYPE_FM:
1357 			osi.synth_type = OSS_SYNTH_TYPE_FM; break;
1358 		case SYNTH_TYPE_SAMPLE:
1359 			osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
1360 		case SYNTH_TYPE_MIDI:
1361 			osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
1362 		default:
1363 			osi.synth_type = 0; break;
1364 		}
1365 		switch(si.synth_subtype) {
1366 		case SYNTH_SUB_FM_TYPE_ADLIB:
1367 			osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
1368 		case SYNTH_SUB_FM_TYPE_OPL3:
1369 			osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
1370 		case SYNTH_SUB_MIDI_TYPE_MPU401:
1371 			osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
1372 		case SYNTH_SUB_SAMPLE_TYPE_BASIC:
1373 			osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
1374 		default:
1375 			osi.synth_subtype = 0; break;
1376 		}
1377 		osi.perc_mode = 0;
1378 		osi.nr_voices = si.nr_voices;
1379 		osi.nr_drums = 0;
1380 		osi.instr_bank_size = si.instr_bank_size;
1381 		osi.capabilities = 0;
1382 		if (si.capabilities & SYNTH_CAP_OPL3)
1383 			osi.capabilities |= OSS_SYNTH_CAP_OPL3;
1384 		if (si.capabilities & SYNTH_CAP_INPUT)
1385 			osi.capabilities |= OSS_SYNTH_CAP_INPUT;
1386 		error = copyout(&osi, SCARG(uap, data), sizeof osi);
1387 		goto out;
1388 	case OSS_SEQ_CTRLRATE:
1389 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1390 		if (error)
1391 			goto out;
1392 		error = ioctlf(fp, SEQUENCER_CTRLRATE, &idat);
1393 		if (error)
1394 			goto out;
1395 		retval[0] = idat;
1396 		break;
1397 	case OSS_SEQ_GETOUTCOUNT:
1398 		error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, &idat);
1399 		if (error)
1400 			goto out;
1401 		retval[0] = idat;
1402 		break;
1403 	case OSS_SEQ_GETINCOUNT:
1404 		error = ioctlf(fp, SEQUENCER_GETINCOUNT, &idat);
1405 		if (error)
1406 			goto out;
1407 		retval[0] = idat;
1408 		break;
1409 	case OSS_SEQ_NRSYNTHS:
1410 		error = ioctlf(fp, SEQUENCER_NRSYNTHS, &idat);
1411 		if (error)
1412 			goto out;
1413 		retval[0] = idat;
1414 		break;
1415 	case OSS_SEQ_NRMIDIS:
1416 		error = ioctlf(fp, SEQUENCER_NRMIDIS, &idat);
1417 		if (error)
1418 			goto out;
1419 		retval[0] = idat;
1420 		break;
1421 	case OSS_SEQ_THRESHOLD:
1422 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1423 		if (error)
1424 			goto out;
1425 		error = ioctlf(fp, SEQUENCER_THRESHOLD, &idat);
1426 		goto out;
1427 	case OSS_MEMAVL:
1428 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1429 		if (error)
1430 			goto out;
1431 		error = ioctlf(fp, SEQUENCER_MEMAVL, &idat);
1432 		if (error)
1433 			goto out;
1434 		retval[0] = idat;
1435 		break;
1436 	case OSS_SEQ_PANIC:
1437 		error = ioctlf(fp, SEQUENCER_PANIC, &idat);
1438 		goto out;
1439 	case OSS_SEQ_OUTOFBAND:
1440 		error = copyin(SCARG(uap, data), &oser, sizeof oser);
1441 		if (error)
1442 			goto out;
1443 		error = ioctlf(fp, SEQUENCER_OUTOFBAND, &oser);
1444 		if (error)
1445 			goto out;
1446 		break;
1447 	case OSS_SEQ_GETTIME:
1448 		error = ioctlf(fp, SEQUENCER_GETTIME, &idat);
1449 		if (error)
1450 			goto out;
1451 		retval[0] = idat;
1452 		break;
1453 	case OSS_TMR_TIMEBASE:
1454 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1455 		if (error)
1456 			goto out;
1457 		error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, &idat);
1458 		if (error)
1459 			goto out;
1460 		retval[0] = idat;
1461 		break;
1462 	case OSS_TMR_START:
1463 		error = ioctlf(fp, SEQUENCER_TMR_START, &idat);
1464 		goto out;
1465 	case OSS_TMR_STOP:
1466 		error = ioctlf(fp, SEQUENCER_TMR_STOP, &idat);
1467 		goto out;
1468 	case OSS_TMR_CONTINUE:
1469 		error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, &idat);
1470 		goto out;
1471 	case OSS_TMR_TEMPO:
1472 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1473 		if (error)
1474 			goto out;
1475 		error = ioctlf(fp, SEQUENCER_TMR_TEMPO, &idat);
1476 		if (error)
1477 			goto out;
1478 		retval[0] = idat;
1479 		break;
1480 	case OSS_TMR_SOURCE:
1481 		error = copyin(SCARG(uap, data), &idat1, sizeof idat);
1482 		if (error)
1483 			goto out;
1484 		idat = 0;
1485 		if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
1486 		error = ioctlf(fp, SEQUENCER_TMR_SOURCE, &idat);
1487 		if (error)
1488 			goto out;
1489 		idat1 = idat;
1490 		if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
1491 		retval[0] = idat;
1492 		break;
1493 	case OSS_TMR_METRONOME:
1494 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1495 		if (error)
1496 			goto out;
1497 		error = ioctlf(fp, SEQUENCER_TMR_METRONOME, &idat);
1498 		goto out;
1499 	case OSS_TMR_SELECT:
1500 		error = copyin(SCARG(uap, data), &idat, sizeof idat);
1501 		if (error)
1502 			goto out;
1503 		retval[0] = idat;
1504 		error = ioctlf(fp, SEQUENCER_TMR_SELECT, &idat);
1505 		goto out;
1506 	default:
1507 		DPRINTF(("%s: Unknown sequencer command 0x%lx\n", __func__,
1508 		    com));
1509 		error = EINVAL;
1510 		goto out;
1511 	}
1512 
1513 	error = copyout(&idat, SCARG(uap, data), sizeof idat);
1514  out:
1515 	fd_putfile(SCARG(uap, fd));
1516 	return error;
1517 }
1518 
1519 /*
1520  * When AUDIO_SETINFO fails to set a channel count, the application's chosen
1521  * number is out of range of what the kernel allows.
1522  *
1523  * When this happens, we use the current hardware settings. This is just in
1524  * case an application is abusing SNDCTL_DSP_CHANNELS - OSSv4 always sets and
1525  * returns a reasonable value, even if it wasn't what the user requested.
1526  *
1527  * XXX: If a device is opened for both playback and recording, and supports
1528  * fewer channels for recording than playback, applications that do both will
1529  * behave very strangely. OSS doesn't allow for reporting separate channel
1530  * counts for recording and playback. This could be worked around by always
1531  * mixing recorded data up to the same number of channels as is being used
1532  * for playback.
1533  */
1534 static void
setchannels(file_t * fp,int mode,int nchannels)1535 setchannels(file_t *fp, int mode, int nchannels)
1536 {
1537 	struct audio_info tmpinfo, hwfmt;
1538 	int (*ioctlf)(file_t *, u_long, void *);
1539 
1540 	ioctlf = fp->f_ops->fo_ioctl;
1541 
1542 	if (ioctlf(fp, AUDIO_GETFORMAT, &hwfmt) < 0) {
1543 		hwfmt.record.channels = hwfmt.play.channels = 2;
1544 	}
1545 
1546 	if (mode & AUMODE_PLAY) {
1547 		AUDIO_INITINFO(&tmpinfo);
1548 		tmpinfo.play.channels = nchannels;
1549 		if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) < 0) {
1550 			AUDIO_INITINFO(&tmpinfo);
1551 			tmpinfo.play.channels = hwfmt.play.channels;
1552 			(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
1553 		}
1554 	}
1555 
1556 	if (mode & AUMODE_RECORD) {
1557 		AUDIO_INITINFO(&tmpinfo);
1558 		tmpinfo.record.channels = nchannels;
1559 		if (ioctlf(fp, AUDIO_SETINFO, &tmpinfo) < 0) {
1560 			AUDIO_INITINFO(&tmpinfo);
1561 			tmpinfo.record.channels = hwfmt.record.channels;
1562 			(void)ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
1563 		}
1564 	}
1565 }
1566 
1567 /*
1568  * Check that the blocksize is a power of 2 as OSS wants.
1569  * If not, set it to be.
1570  */
1571 static void
setblocksize(file_t * fp,struct audio_info * info)1572 setblocksize(file_t *fp, struct audio_info *info)
1573 {
1574 	struct audio_info set;
1575 	u_int s;
1576 
1577 	 if (info->blocksize & (info->blocksize - 1)) {
1578 		for(s = 32; s < info->blocksize; s <<= 1)
1579 			continue;
1580 		AUDIO_INITINFO(&set);
1581 		set.blocksize = s;
1582 		fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, &set);
1583 		fp->f_ops->fo_ioctl(fp, AUDIO_GETBUFINFO, info);
1584 	}
1585 }
1586