Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / ossaudio / ossaudio.c
blobf6b1a22c24e2f697d8ee882c8bc5d64946f827fe
1 /* $NetBSD: ossaudio.c,v 1.64 2008/11/13 10:05:52 ad Exp $ */
3 /*-
4 * Copyright (c) 1997, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ossaudio.c,v 1.64 2008/11/13 10:05:52 ad Exp $");
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>
47 #include <compat/ossaudio/ossaudio.h>
48 #include <compat/ossaudio/ossaudiovar.h>
50 MODULE(MODULE_CLASS_MISC, compat_ossaudio, NULL);
52 #ifdef AUDIO_DEBUG
53 #define DPRINTF(x) if (ossdebug) printf x
54 int ossdebug = 0;
55 #else
56 #define DPRINTF(x)
57 #endif
59 #define TO_OSSVOL(x) (((x) * 100 + 127) / 255)
60 #define FROM_OSSVOL(x) ((((x) > 100 ? 100 : (x)) * 255 + 50) / 100)
62 static struct audiodevinfo *getdevinfo(file_t *);
63 static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq);
64 static int enum_to_ord(struct audiodevinfo *di, int enm);
65 static int enum_to_mask(struct audiodevinfo *di, int enm);
67 static void setblocksize(file_t *, struct audio_info *);
69 #ifdef AUDIO_DEBUG
70 static const char *
71 compat_ossaudio_getcmd(u_long cmd)
73 static char buf[64];
74 switch (cmd) {
75 #define _DO(_a) \
76 case _a: \
77 return # _a;
78 _DO(OSS_SNDCTL_DSP_RESET)
79 _DO(OSS_SNDCTL_DSP_SYNC)
80 _DO(OSS_SNDCTL_DSP_SPEED)
81 _DO(OSS_SOUND_PCM_READ_RATE)
82 _DO(OSS_SNDCTL_DSP_STEREO)
83 _DO(OSS_SNDCTL_DSP_GETBLKSIZE)
84 _DO(OSS_SNDCTL_DSP_SETFMT)
85 _DO(OSS_SOUND_PCM_READ_BITS)
86 _DO(OSS_SNDCTL_DSP_CHANNELS)
87 _DO(OSS_SOUND_PCM_READ_CHANNELS)
88 _DO(OSS_SOUND_PCM_WRITE_FILTER)
89 _DO(OSS_SOUND_PCM_READ_FILTER)
90 _DO(OSS_SNDCTL_DSP_POST)
91 _DO(OSS_SNDCTL_DSP_SUBDIVIDE)
92 _DO(OSS_SNDCTL_DSP_SETFRAGMENT)
93 _DO(OSS_SNDCTL_DSP_GETFMTS)
94 _DO(OSS_SNDCTL_DSP_GETOSPACE)
95 _DO(OSS_SNDCTL_DSP_GETISPACE)
96 _DO(OSS_SNDCTL_DSP_NONBLOCK)
97 _DO(OSS_SNDCTL_DSP_GETCAPS)
98 _DO(OSS_SNDCTL_DSP_GETTRIGGER)
99 _DO(OSS_SNDCTL_DSP_SETTRIGGER)
100 _DO(OSS_SNDCTL_DSP_GETIPTR)
101 _DO(OSS_SNDCTL_DSP_GETOPTR)
102 _DO(OSS_SNDCTL_DSP_MAPINBUF)
103 _DO(OSS_SNDCTL_DSP_MAPOUTBUF)
104 _DO(OSS_SNDCTL_DSP_SETSYNCRO)
105 _DO(OSS_SNDCTL_DSP_SETDUPLEX)
106 _DO(OSS_SNDCTL_DSP_GETODELAY)
107 _DO(OSS_SNDCTL_DSP_PROFILE)
108 _DO(OSS_SOUND_MIXER_INFO)
109 _DO(OSS_SOUND_OLD_MIXER_INFO)
110 _DO(OSS_GET_VERSION)
111 _DO(OSS_SEQ_RESET)
112 _DO(OSS_SEQ_SYNC)
113 _DO(OSS_SYNTH_INFO)
114 _DO(OSS_SEQ_CTRLRATE)
115 _DO(OSS_SEQ_GETOUTCOUNT)
116 _DO(OSS_SEQ_GETINCOUNT)
117 _DO(OSS_SEQ_PERCMODE)
118 _DO(OSS_SEQ_TESTMIDI)
119 _DO(OSS_SEQ_RESETSAMPLES)
120 _DO(OSS_SEQ_NRSYNTHS)
121 _DO(OSS_SEQ_NRMIDIS)
122 #ifdef notyet
123 _DO(OSS_MIDI_INFO)
124 #endif
125 _DO(OSS_SEQ_THRESHOLD)
126 _DO(OSS_MEMAVL)
127 _DO(OSS_FM_4OP_ENABLE)
128 _DO(OSS_SEQ_PANIC)
129 _DO(OSS_SEQ_OUTOFBAND)
130 _DO(OSS_SEQ_GETTIME)
131 _DO(OSS_ID)
132 _DO(OSS_CONTROL)
133 _DO(OSS_REMOVESAMPLE)
134 _DO(OSS_TMR_TIMEBASE)
135 _DO(OSS_TMR_START)
136 _DO(OSS_TMR_STOP)
137 _DO(OSS_TMR_CONTINUE)
138 _DO(OSS_TMR_TEMPO)
139 _DO(OSS_TMR_SOURCE)
140 _DO(OSS_TMR_METRONOME)
141 _DO(OSS_TMR_SELECT)
142 #undef _DO
143 default:
144 (void)snprintf(buf, sizeof(buf), "*0x%lx*", cmd);
145 return buf;
148 #endif
151 static int
152 compat_ossaudio_modcmd(modcmd_t cmd, void *arg)
155 switch (cmd) {
156 case MODULE_CMD_INIT:
157 case MODULE_CMD_FINI:
158 return 0;
159 default:
160 return ENOTTY;
165 oss_ioctl_audio(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
167 /* {
168 syscallarg(int) fd;
169 syscallarg(u_long) com;
170 syscallarg(void *) data;
171 } */
172 file_t *fp;
173 u_long com;
174 struct audio_info tmpinfo;
175 struct audio_offset tmpoffs;
176 struct oss_audio_buf_info bufinfo;
177 struct oss_count_info cntinfo;
178 struct audio_encoding tmpenc;
179 u_int u;
180 int idat, idata;
181 int error = 0;
182 int (*ioctlf)(file_t *, u_long, void *);
184 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
185 return (EBADF);
187 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
188 error = EBADF;
189 goto out;
192 com = SCARG(uap, com);
193 DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
195 retval[0] = 0;
197 ioctlf = fp->f_ops->fo_ioctl;
198 switch (com) {
199 case OSS_SNDCTL_DSP_RESET:
200 error = ioctlf(fp, AUDIO_FLUSH, NULL);
201 if (error) {
202 DPRINTF(("%s: AUDIO_FLUSH %d\n", __func__, error));
203 goto out;
205 break;
206 case OSS_SNDCTL_DSP_SYNC:
207 error = ioctlf(fp, AUDIO_DRAIN, NULL);
208 if (error) {
209 DPRINTF(("%s: AUDIO_DRAIN %d\n", __func__, error));
210 goto out;
212 break;
213 case OSS_SNDCTL_DSP_POST:
214 /* This call is merely advisory, and may be a nop. */
215 break;
216 case OSS_SNDCTL_DSP_SPEED:
217 AUDIO_INITINFO(&tmpinfo);
218 error = copyin(SCARG(uap, data), &idat, sizeof idat);
219 if (error) {
220 DPRINTF(("%s: SNDCTL_DSP_SPEED %d\n",
221 __func__, error));
222 goto out;
224 tmpinfo.play.sample_rate =
225 tmpinfo.record.sample_rate = idat;
226 DPRINTF(("%s: SNDCTL_DSP_SPEED > %d\n", __func__, idat));
227 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
228 if (error) {
229 DPRINTF(("%s: SNDCTL_DSP_SPEED %d = %d\n",
230 __func__, idat, error));
231 goto out;
233 /* fall into ... */
234 case OSS_SOUND_PCM_READ_RATE:
235 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
236 if (error) {
237 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
238 __func__, error));
239 goto out;
241 idat = tmpinfo.play.sample_rate;
242 DPRINTF(("%s: SNDCTL_PCM_READ_RATE < %d\n", __func__, idat));
243 error = copyout(&idat, SCARG(uap, data), sizeof idat);
244 if (error) {
245 DPRINTF(("%s: SOUND_PCM_READ_RATE %d = %d\n",
246 __func__, idat, error));
247 goto out;
249 break;
250 case OSS_SNDCTL_DSP_STEREO:
251 AUDIO_INITINFO(&tmpinfo);
252 error = copyin(SCARG(uap, data), &idat, sizeof idat);
253 if (error) {
254 DPRINTF(("%s: SNDCTL_DSP_STEREO %d\n",
255 __func__, error));
256 goto out;
258 tmpinfo.play.channels =
259 tmpinfo.record.channels = idat ? 2 : 1;
260 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
261 if (error) {
262 DPRINTF(("%s: AUDIO_SETINFO %d\n",
263 __func__, error));
264 goto out;
266 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
267 if (error) {
268 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
269 __func__, error));
270 goto out;
272 idat = tmpinfo.play.channels - 1;
273 error = copyout(&idat, SCARG(uap, data), sizeof idat);
274 if (error) {
275 DPRINTF(("%s: SNDCTL_DSP_STEREO %d = %d\n",
276 __func__, idat, error));
277 goto out;
279 break;
280 case OSS_SNDCTL_DSP_GETBLKSIZE:
281 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
282 if (error) {
283 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
284 __func__, error));
285 goto out;
287 setblocksize(fp, &tmpinfo);
288 idat = tmpinfo.blocksize;
289 DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE < %d\n",
290 __func__, idat));
291 error = copyout(&idat, SCARG(uap, data), sizeof idat);
292 if (error) {
293 DPRINTF(("%s: SNDCTL_DSP_GETBLKSIZE %d = %d\n",
294 __func__, idat, error));
295 goto out;
297 break;
298 case OSS_SNDCTL_DSP_SETFMT:
299 AUDIO_INITINFO(&tmpinfo);
300 error = copyin(SCARG(uap, data), &idat, sizeof idat);
301 if (error) {
302 DPRINTF(("%s: SNDCTL_DSP_SETFMT %d\n",
303 __func__, error));
304 goto out;
306 switch (idat) {
307 case OSS_AFMT_MU_LAW:
308 tmpinfo.play.precision =
309 tmpinfo.record.precision = 8;
310 tmpinfo.play.encoding =
311 tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
312 break;
313 case OSS_AFMT_A_LAW:
314 tmpinfo.play.precision =
315 tmpinfo.record.precision = 8;
316 tmpinfo.play.encoding =
317 tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
318 break;
319 case OSS_AFMT_U8:
320 tmpinfo.play.precision =
321 tmpinfo.record.precision = 8;
322 tmpinfo.play.encoding =
323 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
324 break;
325 case OSS_AFMT_S8:
326 tmpinfo.play.precision =
327 tmpinfo.record.precision = 8;
328 tmpinfo.play.encoding =
329 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
330 break;
331 case OSS_AFMT_S16_LE:
332 tmpinfo.play.precision =
333 tmpinfo.record.precision = 16;
334 tmpinfo.play.encoding =
335 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
336 break;
337 case OSS_AFMT_S16_BE:
338 tmpinfo.play.precision =
339 tmpinfo.record.precision = 16;
340 tmpinfo.play.encoding =
341 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
342 break;
343 case OSS_AFMT_U16_LE:
344 tmpinfo.play.precision =
345 tmpinfo.record.precision = 16;
346 tmpinfo.play.encoding =
347 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
348 break;
349 case OSS_AFMT_U16_BE:
350 tmpinfo.play.precision =
351 tmpinfo.record.precision = 16;
352 tmpinfo.play.encoding =
353 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
354 break;
355 default:
356 DPRINTF(("%s: SNDCTL_DSP_SETFMT bad fmt %d\n",
357 __func__, idat));
358 error = EINVAL;
359 goto out;
361 DPRINTF(("%s: SNDCTL_DSP_SETFMT > 0x%x\n",
362 __func__, idat));
363 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
364 if (error) {
365 DPRINTF(("%s: AUDIO_SETINFO %d\n",
366 __func__, error));
367 goto out;
369 /* fall into ... */
370 case OSS_SOUND_PCM_READ_BITS:
371 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
372 if (error) {
373 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
374 __func__, error));
375 goto out;
377 switch (tmpinfo.play.encoding) {
378 case AUDIO_ENCODING_ULAW:
379 idat = OSS_AFMT_MU_LAW;
380 break;
381 case AUDIO_ENCODING_ALAW:
382 idat = OSS_AFMT_A_LAW;
383 break;
384 case AUDIO_ENCODING_SLINEAR_LE:
385 if (tmpinfo.play.precision == 16)
386 idat = OSS_AFMT_S16_LE;
387 else
388 idat = OSS_AFMT_S8;
389 break;
390 case AUDIO_ENCODING_SLINEAR_BE:
391 if (tmpinfo.play.precision == 16)
392 idat = OSS_AFMT_S16_BE;
393 else
394 idat = OSS_AFMT_S8;
395 break;
396 case AUDIO_ENCODING_ULINEAR_LE:
397 if (tmpinfo.play.precision == 16)
398 idat = OSS_AFMT_U16_LE;
399 else
400 idat = OSS_AFMT_U8;
401 break;
402 case AUDIO_ENCODING_ULINEAR_BE:
403 if (tmpinfo.play.precision == 16)
404 idat = OSS_AFMT_U16_BE;
405 else
406 idat = OSS_AFMT_U8;
407 break;
408 case AUDIO_ENCODING_ADPCM:
409 idat = OSS_AFMT_IMA_ADPCM;
410 break;
411 default:
412 DPRINTF(("%s: SOUND_PCM_READ_BITS bad encoding %d\n",
413 __func__, tmpinfo.play.encoding));
414 error = EINVAL;
415 goto out;
417 DPRINTF(("%s: SOUND_PCM_READ_BITS < 0x%x\n",
418 __func__, idat));
419 error = copyout(&idat, SCARG(uap, data), sizeof idat);
420 if (error) {
421 DPRINTF(("%s: SOUND_PCM_READ_BITS %d = %d\n",
422 __func__, idat, error));
423 goto out;
425 break;
426 case OSS_SNDCTL_DSP_CHANNELS:
427 AUDIO_INITINFO(&tmpinfo);
428 error = copyin(SCARG(uap, data), &idat, sizeof idat);
429 if (error) {
430 DPRINTF(("%s: SNDCTL_DSP_CHANNELS %d\n",
431 __func__, error));
432 goto out;
434 tmpinfo.play.channels =
435 tmpinfo.record.channels = idat;
436 DPRINTF(("%s: SNDCTL_DSP_CHANNELS > %d\n", __func__, idat));
437 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
438 if (error) {
439 DPRINTF(("%s: AUDIO_SETINFO %d\n",
440 __func__, error));
441 goto out;
443 /* fall into ... */
444 case OSS_SOUND_PCM_READ_CHANNELS:
445 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
446 if (error) {
447 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
448 __func__, error));
449 goto out;
451 idat = tmpinfo.play.channels;
452 DPRINTF(("%s: SOUND_PCM_READ_CHANNELS < %d\n", __func__, idat));
453 error = copyout(&idat, SCARG(uap, data), sizeof idat);
454 if (error) {
455 DPRINTF(("%s: SOUND_PCM_READ_CHANNELS %d = %d\n",
456 __func__, idat, error));
457 goto out;
459 break;
460 case OSS_SOUND_PCM_WRITE_FILTER:
461 case OSS_SOUND_PCM_READ_FILTER:
462 error = EINVAL; /* XXX unimplemented */
463 DPRINTF(("%s: SOUND_PCM_{READ,WRITE}_FILTER filter\n",
464 __func__));
465 goto out;
466 case OSS_SNDCTL_DSP_SUBDIVIDE:
467 error = copyin(SCARG(uap, data), &idat, sizeof idat);
468 if (error) {
469 DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d\n",
470 __func__, error));
471 goto out;
473 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
474 if (error) {
475 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
476 __func__, error));
477 goto out;
479 setblocksize(fp, &tmpinfo);
480 if (idat == 0)
481 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
482 idat = (tmpinfo.play.buffer_size / idat) & -4;
483 AUDIO_INITINFO(&tmpinfo);
484 tmpinfo.blocksize = idat;
485 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
486 if (error) {
487 DPRINTF(("%s: AUDIO_SETINFO %d\n",
488 __func__, error));
489 goto out;
491 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
492 error = copyout(&idat, SCARG(uap, data), sizeof idat);
493 if (error) {
494 DPRINTF(("%s: SNDCTL_DSP_SUBDIVIDE %d = %d\n",
495 __func__, idat, error));
496 goto out;
498 break;
499 case OSS_SNDCTL_DSP_SETFRAGMENT:
500 AUDIO_INITINFO(&tmpinfo);
501 error = copyin(SCARG(uap, data), &idat, sizeof idat);
502 if (error) {
503 DPRINTF(("%s: DSP_SETFRAGMENT %d\n",
504 __func__, error));
505 goto out;
507 if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
508 DPRINTF(("%s: DSP_SETFRAGMENT bad ival%d\n",
509 __func__, idat));
510 error = EINVAL;
511 goto out;
513 tmpinfo.blocksize = 1 << (idat & 0xffff);
514 tmpinfo.hiwat = (idat >> 16) & 0x7fff;
515 DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT blksize=%d, "
516 "hiwat=%d\n", __func__, tmpinfo.blocksize, tmpinfo.hiwat));
517 if (tmpinfo.hiwat == 0) /* 0 means set to max */
518 tmpinfo.hiwat = 65536;
519 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
520 if (error) {
521 DPRINTF(("%s: AUDIO_SETINFO %d\n",
522 __func__, error));
523 goto out;
525 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
526 if (error) {
527 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
528 __func__, error));
529 goto out;
531 u = tmpinfo.blocksize;
532 for(idat = 0; u > 1; idat++, u >>= 1)
534 idat |= (tmpinfo.hiwat & 0x7fff) << 16;
535 error = copyout(&idat, SCARG(uap, data), sizeof idat);
536 if (error) {
537 DPRINTF(("%s: SNDCTL_DSP_SETFRAGMENT %d = %d\n",
538 __func__, idat, error));
539 goto out;
541 break;
542 case OSS_SNDCTL_DSP_GETFMTS:
543 for (idat = 0, tmpenc.index = 0;
544 ioctlf(fp, AUDIO_GETENC, &tmpenc) == 0;
545 tmpenc.index++) {
546 switch(tmpenc.encoding) {
547 case AUDIO_ENCODING_ULAW:
548 idat |= OSS_AFMT_MU_LAW;
549 break;
550 case AUDIO_ENCODING_ALAW:
551 idat |= OSS_AFMT_A_LAW;
552 break;
553 case AUDIO_ENCODING_SLINEAR:
554 idat |= OSS_AFMT_S8;
555 break;
556 case AUDIO_ENCODING_SLINEAR_LE:
557 if (tmpenc.precision == 16)
558 idat |= OSS_AFMT_S16_LE;
559 else
560 idat |= OSS_AFMT_S8;
561 break;
562 case AUDIO_ENCODING_SLINEAR_BE:
563 if (tmpenc.precision == 16)
564 idat |= OSS_AFMT_S16_BE;
565 else
566 idat |= OSS_AFMT_S8;
567 break;
568 case AUDIO_ENCODING_ULINEAR:
569 idat |= OSS_AFMT_U8;
570 break;
571 case AUDIO_ENCODING_ULINEAR_LE:
572 if (tmpenc.precision == 16)
573 idat |= OSS_AFMT_U16_LE;
574 else
575 idat |= OSS_AFMT_U8;
576 break;
577 case AUDIO_ENCODING_ULINEAR_BE:
578 if (tmpenc.precision == 16)
579 idat |= OSS_AFMT_U16_BE;
580 else
581 idat |= OSS_AFMT_U8;
582 break;
583 case AUDIO_ENCODING_ADPCM:
584 idat |= OSS_AFMT_IMA_ADPCM;
585 break;
586 default:
587 DPRINTF(("%s: SNDCTL_DSP_GETFMTS unknown %d\n",
588 __func__, tmpenc.encoding));
589 break;
592 DPRINTF(("%s: SNDCTL_DSP_GETFMTS < 0x%x\n",
593 __func__, idat));
594 error = copyout(&idat, SCARG(uap, data), sizeof idat);
595 if (error) {
596 DPRINTF(("%s: SNDCTL_DSP_GETFMTS = %x = %d\n",
597 __func__, idat, error));
598 goto out;
600 break;
601 case OSS_SNDCTL_DSP_GETOSPACE:
602 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
603 if (error) {
604 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
605 __func__, error));
606 goto out;
608 setblocksize(fp, &tmpinfo);
609 bufinfo.fragsize = tmpinfo.blocksize;
610 bufinfo.fragments = tmpinfo.hiwat -
611 (tmpinfo.play.seek + tmpinfo.blocksize - 1) /
612 tmpinfo.blocksize;
613 bufinfo.fragstotal = tmpinfo.hiwat;
614 bufinfo.bytes =
615 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
616 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
617 if (error) {
618 DPRINTF(("%s: SNDCTL_DSP_GETOSPACE = %d\n",
619 __func__, error));
620 goto out;
622 break;
623 case OSS_SNDCTL_DSP_GETISPACE:
624 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
625 if (error) {
626 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
627 __func__, error));
628 goto out;
630 setblocksize(fp, &tmpinfo);
631 bufinfo.fragsize = tmpinfo.blocksize;
632 bufinfo.fragments = tmpinfo.hiwat -
633 (tmpinfo.record.seek + tmpinfo.blocksize - 1) /
634 tmpinfo.blocksize;
635 bufinfo.fragstotal = tmpinfo.hiwat;
636 bufinfo.bytes =
637 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.record.seek;
638 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
639 if (error) {
640 DPRINTF(("%s: SNDCTL_DSP_GETISPACE %d %d %d %d = %d\n",
641 __func__, bufinfo.fragsize, bufinfo.fragments,
642 bufinfo.fragstotal, bufinfo.bytes, error));
643 goto out;
645 break;
646 case OSS_SNDCTL_DSP_NONBLOCK:
647 idat = 1;
648 error = ioctlf(fp, FIONBIO, &idat);
649 if (error) {
650 DPRINTF(("%s: SENDCLT_DSP_NONBLOCK %d\n",
651 __func__, error));
652 goto out;
654 break;
655 case OSS_SNDCTL_DSP_GETCAPS:
656 error = ioctlf(fp, AUDIO_GETPROPS, &idata);
657 if (error) {
658 DPRINTF(("%s: AUDIO_GETPROPS %d\n",
659 __func__, error));
660 goto out;
662 idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
663 if (idata & AUDIO_PROP_FULLDUPLEX)
664 idat |= OSS_DSP_CAP_DUPLEX;
665 if (idata & AUDIO_PROP_MMAP)
666 idat |= OSS_DSP_CAP_MMAP;
667 DPRINTF(("%s: SNDCL_DSP_GETCAPS %s duplex, %smmap\n",
668 __func__, (idat & OSS_DSP_CAP_DUPLEX) ? "full" : "half",
669 (idat & OSS_DSP_CAP_MMAP) ? "" : "no "));
670 error = copyout(&idat, SCARG(uap, data), sizeof idat);
671 if (error) {
672 DPRINTF(("%s: SNDCTL_DSP_GETCAPS %x = %d\n", __func__,
673 idat, error));
674 goto out;
676 break;
677 #if 0
678 case OSS_SNDCTL_DSP_GETTRIGGER:
679 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
680 if (error) {
681 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
682 __func__, error));
683 goto out;
685 idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
686 (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
687 error = copyout(&idat, SCARG(uap, data), sizeof idat);
688 if (error) {
689 DPRINTF(("%s: SNDCTL_DSP_SETRIGGER %x = %d\n",
690 __func__, idat, error));
691 goto out;
693 break;
694 case OSS_SNDCTL_DSP_SETTRIGGER:
695 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo, p);
696 if (error) {
697 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
698 __func__, error));
699 goto out;
701 error = copyin(SCARG(uap, data), &idat, sizeof idat);
702 if (error) {
703 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
704 __func__, error));
705 goto out;
707 tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
708 tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
709 error = ioctlf(fp, AUDIO_SETINFO, &tmpinfo);
710 if (error) {
711 DPRINTF(("%s: AUDIO_SETINFO %d\n",
712 __func__, error));
713 goto out;
715 error = copyout(&idat, SCARG(uap, data), sizeof idat);
716 if (error) {
717 DPRINTF(("%s: SNDCTL_DSP_SETRIGGER %x = %d\n",
718 __func__, idat, error));
719 goto out;
721 break;
722 #else
723 case OSS_SNDCTL_DSP_GETTRIGGER:
724 case OSS_SNDCTL_DSP_SETTRIGGER:
725 /* XXX Do nothing for now. */
726 idat = OSS_PCM_ENABLE_OUTPUT;
727 error = copyout(&idat, SCARG(uap, data), sizeof idat);
728 if (error) {
729 DPRINTF(("%s: SNDCTL_DSP_{GET,SET}RIGGER %x = %d\n",
730 __func__, idat, error));
731 goto out;
733 break;
734 #endif
735 case OSS_SNDCTL_DSP_GETIPTR:
736 error = ioctlf(fp, AUDIO_GETIOFFS, &tmpoffs);
737 if (error) {
738 DPRINTF(("%s: AUDIO_GETIOFFS %d\n",
739 __func__, error));
740 goto out;
742 cntinfo.bytes = tmpoffs.samples;
743 cntinfo.blocks = tmpoffs.deltablks;
744 cntinfo.ptr = tmpoffs.offset;
745 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
746 if (error) {
747 DPRINTF(("%s: SNDCTL_DSP_GETIPTR %d\n",
748 __func__, error));
749 goto out;
751 break;
752 case OSS_SNDCTL_DSP_GETOPTR:
753 error = ioctlf(fp, AUDIO_GETOOFFS, &tmpoffs);
754 if (error) {
755 DPRINTF(("%s: AUDIO_GETOOFFS %d\n",
756 __func__, error));
757 goto out;
759 cntinfo.bytes = tmpoffs.samples;
760 cntinfo.blocks = tmpoffs.deltablks;
761 cntinfo.ptr = tmpoffs.offset;
762 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
763 if (error) {
764 DPRINTF(("%s: SNDCTL_DSP_GETOPTR %d\n",
765 __func__, error));
766 goto out;
768 break;
769 case OSS_SNDCTL_DSP_SETDUPLEX:
770 idat = 1;
771 error = ioctlf(fp, AUDIO_SETFD, &idat);
772 if (error) {
773 DPRINTF(("%s: AUDIO_SETFD %d = %d\n",
774 __func__, idat, error));
775 goto out;
777 break;
778 case OSS_SNDCTL_DSP_MAPINBUF:
779 DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPINBUF\n",
780 __func__));
781 error = EINVAL;
782 goto out;
783 case OSS_SNDCTL_DSP_MAPOUTBUF:
784 DPRINTF(("%s: Unimplemented SNDCTL_DSP_MAPOUTBUF\n",
785 __func__));
786 error = EINVAL;
787 goto out;
788 case OSS_SNDCTL_DSP_SETSYNCRO:
789 DPRINTF(("%s: Unimplemented SNDCTL_DSP_GETSYNCHRO\n",
790 __func__));
791 error = EINVAL;
792 goto out;
793 case OSS_SNDCTL_DSP_GETODELAY:
794 error = ioctlf(fp, AUDIO_GETBUFINFO, &tmpinfo);
795 if (error) {
796 DPRINTF(("%s: AUDIO_GETBUFINFO %d\n",
797 __func__, error));
798 goto out;
800 idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
801 error = copyout(&idat, SCARG(uap, data), sizeof idat);
802 if (error) {
803 DPRINTF(("%s: SNDCTL_DSP_GETODELAY %d\n",
804 __func__, error));
805 goto out;
807 break;
808 case OSS_SNDCTL_DSP_PROFILE:
809 /* This gives just a hint to the driver,
810 * implementing it as a NOP is ok
812 DPRINTF(("%s: SNDCTL_DSP_PROFILE\n", __func__));
813 break;
814 default:
815 DPRINTF(("%s: Unimplemented 0x%lx\n", __func__, com));
816 error = EINVAL;
817 goto out;
820 out:
821 fd_putfile(SCARG(uap, fd));
822 return error;
825 /* If the NetBSD mixer device should have more than 32 devices
826 * some will not be available to Linux */
827 #define NETBSD_MAXDEVS 64
828 struct audiodevinfo {
829 int done;
830 dev_t dev;
831 int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
832 rdevmap[NETBSD_MAXDEVS];
833 char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
834 int enum2opaque[NETBSD_MAXDEVS];
835 u_long devmask, recmask, stereomask;
836 u_long caps, source;
839 static int
840 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
842 int i, o;
844 for (i = 0; i < NETBSD_MAXDEVS; i++) {
845 o = di->enum2opaque[i];
846 if (o == opq)
847 break;
848 if (o == -1 && label != NULL &&
849 !strncmp(di->names[i], label->name, sizeof di->names[i])) {
850 di->enum2opaque[i] = opq;
851 break;
854 if (i >= NETBSD_MAXDEVS)
855 i = -1;
856 /*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
857 return (i);
860 static int
861 enum_to_ord(struct audiodevinfo *di, int enm)
863 if (enm >= NETBSD_MAXDEVS)
864 return (-1);
866 /*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
867 return (di->enum2opaque[enm]);
870 static int
871 enum_to_mask(struct audiodevinfo *di, int enm)
873 int m;
874 if (enm >= NETBSD_MAXDEVS)
875 return (0);
877 m = di->enum2opaque[enm];
878 if (m == -1)
879 m = 0;
880 /*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
881 return (m);
885 * Collect the audio device information to allow faster
886 * emulation of the Linux mixer ioctls. Cache the information
887 * to eliminate the overhead of repeating all the ioctls needed
888 * to collect the information.
890 static struct audiodevinfo *
891 getdevinfo(file_t *fp)
893 mixer_devinfo_t mi;
894 int i, j, e;
895 static const struct {
896 const char *name;
897 int code;
898 } *dp, devs[] = {
899 { AudioNmicrophone, OSS_SOUND_MIXER_MIC },
900 { AudioNline, OSS_SOUND_MIXER_LINE },
901 { AudioNcd, OSS_SOUND_MIXER_CD },
902 { AudioNdac, OSS_SOUND_MIXER_PCM },
903 { AudioNaux, OSS_SOUND_MIXER_LINE1 },
904 { AudioNrecord, OSS_SOUND_MIXER_IMIX },
905 { AudioNmaster, OSS_SOUND_MIXER_VOLUME },
906 { AudioNtreble, OSS_SOUND_MIXER_TREBLE },
907 { AudioNbass, OSS_SOUND_MIXER_BASS },
908 { AudioNspeaker, OSS_SOUND_MIXER_SPEAKER },
909 /* { AudioNheadphone, ?? },*/
910 { AudioNoutput, OSS_SOUND_MIXER_OGAIN },
911 { AudioNinput, OSS_SOUND_MIXER_IGAIN },
912 /* { AudioNmaster, OSS_SOUND_MIXER_SPEAKER },*/
913 /* { AudioNstereo, ?? },*/
914 /* { AudioNmono, ?? },*/
915 { AudioNfmsynth, OSS_SOUND_MIXER_SYNTH },
916 /* { AudioNwave, OSS_SOUND_MIXER_PCM },*/
917 { AudioNmidi, OSS_SOUND_MIXER_SYNTH },
918 /* { AudioNmixerout, ?? },*/
919 { 0, -1 }
921 int (*ioctlf)(file_t *, u_long, void *) = fp->f_ops->fo_ioctl;
922 struct vnode *vp;
923 struct vattr va;
924 static struct audiodevinfo devcache;
925 struct audiodevinfo *di = &devcache;
926 int mlen, dlen;
929 * Figure out what device it is so we can check if the
930 * cached data is valid.
932 vp = fp->f_data;
933 if (vp->v_type != VCHR)
934 return 0;
935 if (VOP_GETATTR(vp, &va, kauth_cred_get()))
936 return 0;
937 if (di->done && di->dev == va.va_rdev)
938 return di;
940 di->done = 1;
941 di->dev = va.va_rdev;
942 di->devmask = 0;
943 di->recmask = 0;
944 di->stereomask = 0;
945 di->source = ~0;
946 di->caps = 0;
947 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
948 di->devmap[i] = -1;
949 for(i = 0; i < NETBSD_MAXDEVS; i++) {
950 di->rdevmap[i] = -1;
951 di->names[i][0] = '\0';
952 di->enum2opaque[i] = -1;
954 for(i = 0; i < NETBSD_MAXDEVS; i++) {
955 mi.index = i;
956 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) < 0)
957 break;
958 switch(mi.type) {
959 case AUDIO_MIXER_VALUE:
960 for(dp = devs; dp->name; dp++) {
961 if (strcmp(dp->name, mi.label.name) == 0)
962 break;
963 dlen = strlen(dp->name);
964 mlen = strlen(mi.label.name);
965 if (dlen < mlen
966 && mi.label.name[mlen-dlen-1] == '.'
967 && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
968 break;
970 if (dp->code >= 0) {
971 di->devmap[dp->code] = i;
972 di->rdevmap[i] = dp->code;
973 di->devmask |= 1 << dp->code;
974 if (mi.un.v.num_channels == 2)
975 di->stereomask |= 1 << dp->code;
976 strncpy(di->names[i], mi.label.name,
977 sizeof di->names[i]);
979 break;
982 for(i = 0; i < NETBSD_MAXDEVS; i++) {
983 mi.index = i;
984 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, &mi) < 0)
985 break;
986 if (strcmp(mi.label.name, AudioNsource) != 0)
987 continue;
988 di->source = i;
989 switch(mi.type) {
990 case AUDIO_MIXER_ENUM:
991 for(j = 0; j < mi.un.e.num_mem; j++) {
992 e = opaque_to_enum(di,
993 &mi.un.e.member[j].label,
994 mi.un.e.member[j].ord);
995 if (e >= 0)
996 di->recmask |= 1 << di->rdevmap[e];
998 di->caps = OSS_SOUND_CAP_EXCL_INPUT;
999 break;
1000 case AUDIO_MIXER_SET:
1001 for(j = 0; j < mi.un.s.num_mem; j++) {
1002 e = opaque_to_enum(di,
1003 &mi.un.s.member[j].label,
1004 mi.un.s.member[j].mask);
1005 if (e >= 0)
1006 di->recmask |= 1 << di->rdevmap[e];
1008 break;
1011 return di;
1015 oss_ioctl_mixer(struct lwp *lwp, const struct oss_sys_ioctl_args *uap, register_t *retval)
1017 /* {
1018 syscallarg(int) fd;
1019 syscallarg(u_long) com;
1020 syscallarg(void *) data;
1021 } */
1022 file_t *fp;
1023 u_long com;
1024 struct audiodevinfo *di;
1025 mixer_ctrl_t mc;
1026 struct oss_mixer_info omi;
1027 struct audio_device adev;
1028 int idat;
1029 int i;
1030 int error;
1031 int l, r, n, e;
1032 int (*ioctlf)(file_t *, u_long, void *);
1034 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1035 return (EBADF);
1037 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1038 error = EBADF;
1039 goto out;
1042 com = SCARG(uap, com);
1043 DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1045 retval[0] = 0;
1047 di = getdevinfo(fp);
1048 if (di == 0) {
1049 error = EINVAL;
1050 goto out;
1053 ioctlf = fp->f_ops->fo_ioctl;
1054 switch (com) {
1055 case OSS_GET_VERSION:
1056 idat = OSS_SOUND_VERSION;
1057 break;
1058 case OSS_SOUND_MIXER_INFO:
1059 case OSS_SOUND_OLD_MIXER_INFO:
1060 error = ioctlf(fp, AUDIO_GETDEV, &adev);
1061 if (error) {
1062 DPRINTF(("%s: AUDIO_GETDEV %d\n",
1063 __func__, error));
1064 goto out;
1066 omi.modify_counter = 1;
1067 strncpy(omi.id, adev.name, sizeof omi.id);
1068 strncpy(omi.name, adev.name, sizeof omi.name);
1069 error = copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
1070 if (error) {
1071 DPRINTF(("%s: OSS_SOUND_MIXER_INFO %d\n",
1072 __func__, error));
1073 goto out;
1075 break;
1076 case OSS_SOUND_MIXER_READ_RECSRC:
1077 if (di->source == -1) {
1078 DPRINTF(("%s: OSS_SOUND_MIXER_READ_RECSRC bad source\n",
1079 __func__));
1080 error = EINVAL;
1081 goto out;
1083 mc.dev = di->source;
1084 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1085 mc.type = AUDIO_MIXER_ENUM;
1086 error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1087 if (error) {
1088 DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1089 __func__, error));
1090 goto out;
1092 e = opaque_to_enum(di, NULL, mc.un.ord);
1093 if (e >= 0)
1094 idat = 1 << di->rdevmap[e];
1095 } else {
1096 mc.type = AUDIO_MIXER_SET;
1097 error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1098 if (error) {
1099 DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1100 __func__, error));
1101 goto out;
1103 e = opaque_to_enum(di, NULL, mc.un.mask);
1104 if (e >= 0)
1105 idat = 1 << di->rdevmap[e];
1107 break;
1108 case OSS_SOUND_MIXER_READ_DEVMASK:
1109 idat = di->devmask;
1110 break;
1111 case OSS_SOUND_MIXER_READ_RECMASK:
1112 idat = di->recmask;
1113 break;
1114 case OSS_SOUND_MIXER_READ_STEREODEVS:
1115 idat = di->stereomask;
1116 break;
1117 case OSS_SOUND_MIXER_READ_CAPS:
1118 idat = di->caps;
1119 break;
1120 case OSS_SOUND_MIXER_WRITE_RECSRC:
1121 case OSS_SOUND_MIXER_WRITE_R_RECSRC:
1122 if (di->source == -1) {
1123 DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC bad "
1124 "source\n", __func__));
1125 error = EINVAL;
1126 goto out;
1128 mc.dev = di->source;
1129 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1130 if (error) {
1131 DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC %d\n",
1132 __func__, error));
1133 goto out;
1135 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
1136 mc.type = AUDIO_MIXER_ENUM;
1137 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
1138 if (idat & (1 << i))
1139 break;
1140 if (i >= OSS_SOUND_MIXER_NRDEVICES ||
1141 di->devmap[i] == -1) {
1142 error = EINVAL;
1143 DPRINTF(("%s: OSS_SOUND_MIXER_WRITE_RECSRC "
1144 "bad index %d\n", __func__, i));
1145 goto out;
1147 mc.un.ord = enum_to_ord(di, di->devmap[i]);
1148 } else {
1149 mc.type = AUDIO_MIXER_SET;
1150 mc.un.mask = 0;
1151 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
1152 if (idat & (1 << i)) {
1153 if (di->devmap[i] == -1) {
1154 DPRINTF(("%s: OSS_SOUND_MIXER_"
1155 "WRITE_RECSRC bad devmap "
1156 "%d\n", __func__, i));
1157 error = EINVAL;
1158 goto out;
1160 mc.un.mask |= enum_to_mask(di,
1161 di->devmap[i]);
1165 error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1166 if (error) {
1167 DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1168 __func__, error));
1169 goto out;
1171 goto out;
1172 default:
1173 if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
1174 com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
1175 n = OSS_GET_DEV(com);
1176 if (di->devmap[n] == -1) {
1177 DPRINTF(("%s: 0x%lx bad devmap %d\n",
1178 __func__, com, n));
1179 error = EINVAL;
1180 goto out;
1182 doread:
1183 mc.dev = di->devmap[n];
1184 mc.type = AUDIO_MIXER_VALUE;
1185 mc.un.value.num_channels = di->stereomask &
1186 (1 << n) ? 2 : 1;
1187 error = ioctlf(fp, AUDIO_MIXER_READ, &mc);
1188 if (error) {
1189 DPRINTF(("%s: AUDIO_MIXER_READ %d\n",
1190 __func__, error));
1191 goto out;
1193 if (mc.un.value.num_channels != 2) {
1194 l = r =
1195 mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
1196 } else {
1197 l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
1198 r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
1200 idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
1201 DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1202 __func__, n, di->devmap[n], l, r, idat));
1203 break;
1204 } else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
1205 com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
1206 (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1207 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
1208 n = OSS_GET_DEV(com);
1209 if (di->devmap[n] == -1) {
1210 DPRINTF(("%s: 0x%lx bad devmap %d\n",
1211 __func__, com, n));
1212 error = EINVAL;
1213 goto out;
1215 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1216 if (error) {
1217 DPRINTF(("%s: 0x%lx error %d\n",
1218 __func__, com, error));
1219 goto out;
1221 l = FROM_OSSVOL( idat & 0xff);
1222 r = FROM_OSSVOL((idat >> 8) & 0xff);
1223 mc.dev = di->devmap[n];
1224 mc.type = AUDIO_MIXER_VALUE;
1225 if (di->stereomask & (1 << n)) {
1226 mc.un.value.num_channels = 2;
1227 mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
1228 mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
1229 } else {
1230 mc.un.value.num_channels = 1;
1231 mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] =
1232 (l + r) / 2;
1234 DPRINTF(("%s: n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
1235 __func__, n, di->devmap[n], l, r, idat));
1236 error = ioctlf(fp, AUDIO_MIXER_WRITE, &mc);
1237 if (error) {
1238 DPRINTF(("%s: AUDIO_MIXER_WRITE %d\n",
1239 __func__, error));
1240 goto out;
1242 if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
1243 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
1244 error = 0;
1245 goto out;
1247 goto doread;
1248 } else {
1249 DPRINTF(("%s: Unknown mixer ioctl 0x%lx\n", __func__,
1250 com));
1251 error = EINVAL;
1252 goto out;
1255 error = copyout(&idat, SCARG(uap, data), sizeof idat);
1256 out:
1257 fd_putfile(SCARG(uap, fd));
1258 return error;
1261 /* Sequencer emulation */
1263 oss_ioctl_sequencer(struct lwp *l, const struct oss_sys_ioctl_args *uap, register_t *retval)
1265 /* {
1266 syscallarg(int) fd;
1267 syscallarg(u_long) com;
1268 syscallarg(void *) data;
1269 } */
1270 file_t *fp;
1271 u_long com;
1272 int idat, idat1;
1273 struct synth_info si;
1274 struct oss_synth_info osi;
1275 struct oss_seq_event_rec oser;
1276 int error;
1277 int (*ioctlf)(file_t *, u_long, void *);
1279 if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1280 return (EBADF);
1282 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1283 error = EBADF;
1284 goto out;
1287 com = SCARG(uap, com);
1288 DPRINTF(("%s: com=%s\n", __func__, compat_ossaudio_getcmd(com)));
1290 retval[0] = 0;
1292 ioctlf = fp->f_ops->fo_ioctl;
1293 switch (com) {
1294 case OSS_SEQ_RESET:
1295 error = ioctlf(fp, SEQUENCER_RESET, &idat);
1296 goto out;
1297 case OSS_SEQ_SYNC:
1298 error = ioctlf(fp, SEQUENCER_SYNC, &idat);
1299 goto out;
1300 case OSS_SYNTH_INFO:
1301 error = copyin(SCARG(uap, data), &osi, sizeof osi);
1302 if (error)
1303 goto out;
1304 si.device = osi.device;
1305 error = ioctlf(fp, SEQUENCER_INFO, &si);
1306 if (error)
1307 goto out;
1308 strncpy(osi.name, si.name, sizeof osi.name);
1309 osi.device = si.device;
1310 switch(si.synth_type) {
1311 case SYNTH_TYPE_FM:
1312 osi.synth_type = OSS_SYNTH_TYPE_FM; break;
1313 case SYNTH_TYPE_SAMPLE:
1314 osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
1315 case SYNTH_TYPE_MIDI:
1316 osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
1317 default:
1318 osi.synth_type = 0; break;
1320 switch(si.synth_subtype) {
1321 case SYNTH_SUB_FM_TYPE_ADLIB:
1322 osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
1323 case SYNTH_SUB_FM_TYPE_OPL3:
1324 osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
1325 case SYNTH_SUB_MIDI_TYPE_MPU401:
1326 osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
1327 case SYNTH_SUB_SAMPLE_TYPE_BASIC:
1328 osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
1329 default:
1330 osi.synth_subtype = 0; break;
1332 osi.perc_mode = 0;
1333 osi.nr_voices = si.nr_voices;
1334 osi.nr_drums = 0;
1335 osi.instr_bank_size = si.instr_bank_size;
1336 osi.capabilities = 0;
1337 if (si.capabilities & SYNTH_CAP_OPL3)
1338 osi.capabilities |= OSS_SYNTH_CAP_OPL3;
1339 if (si.capabilities & SYNTH_CAP_INPUT)
1340 osi.capabilities |= OSS_SYNTH_CAP_INPUT;
1341 error = copyout(&osi, SCARG(uap, data), sizeof osi);
1342 goto out;
1343 case OSS_SEQ_CTRLRATE:
1344 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1345 if (error)
1346 goto out;
1347 error = ioctlf(fp, SEQUENCER_CTRLRATE, &idat);
1348 if (error)
1349 goto out;
1350 retval[0] = idat;
1351 break;
1352 case OSS_SEQ_GETOUTCOUNT:
1353 error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, &idat);
1354 if (error)
1355 goto out;
1356 retval[0] = idat;
1357 break;
1358 case OSS_SEQ_GETINCOUNT:
1359 error = ioctlf(fp, SEQUENCER_GETINCOUNT, &idat);
1360 if (error)
1361 goto out;
1362 retval[0] = idat;
1363 break;
1364 case OSS_SEQ_NRSYNTHS:
1365 error = ioctlf(fp, SEQUENCER_NRSYNTHS, &idat);
1366 if (error)
1367 goto out;
1368 retval[0] = idat;
1369 break;
1370 case OSS_SEQ_NRMIDIS:
1371 error = ioctlf(fp, SEQUENCER_NRMIDIS, &idat);
1372 if (error)
1373 goto out;
1374 retval[0] = idat;
1375 break;
1376 case OSS_SEQ_THRESHOLD:
1377 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1378 if (error)
1379 goto out;
1380 error = ioctlf(fp, SEQUENCER_THRESHOLD, &idat);
1381 goto out;
1382 case OSS_MEMAVL:
1383 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1384 if (error)
1385 goto out;
1386 error = ioctlf(fp, SEQUENCER_MEMAVL, &idat);
1387 if (error)
1388 goto out;
1389 retval[0] = idat;
1390 break;
1391 case OSS_SEQ_PANIC:
1392 error = ioctlf(fp, SEQUENCER_PANIC, &idat);
1393 goto out;
1394 case OSS_SEQ_OUTOFBAND:
1395 error = copyin(SCARG(uap, data), &oser, sizeof oser);
1396 if (error)
1397 goto out;
1398 error = ioctlf(fp, SEQUENCER_OUTOFBAND, &oser);
1399 if (error)
1400 goto out;
1401 break;
1402 case OSS_SEQ_GETTIME:
1403 error = ioctlf(fp, SEQUENCER_GETTIME, &idat);
1404 if (error)
1405 goto out;
1406 retval[0] = idat;
1407 break;
1408 case OSS_TMR_TIMEBASE:
1409 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1410 if (error)
1411 goto out;
1412 error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, &idat);
1413 if (error)
1414 goto out;
1415 retval[0] = idat;
1416 break;
1417 case OSS_TMR_START:
1418 error = ioctlf(fp, SEQUENCER_TMR_START, &idat);
1419 goto out;
1420 case OSS_TMR_STOP:
1421 error = ioctlf(fp, SEQUENCER_TMR_STOP, &idat);
1422 goto out;
1423 case OSS_TMR_CONTINUE:
1424 error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, &idat);
1425 goto out;
1426 case OSS_TMR_TEMPO:
1427 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1428 if (error)
1429 goto out;
1430 error = ioctlf(fp, SEQUENCER_TMR_TEMPO, &idat);
1431 if (error)
1432 goto out;
1433 retval[0] = idat;
1434 break;
1435 case OSS_TMR_SOURCE:
1436 error = copyin(SCARG(uap, data), &idat1, sizeof idat);
1437 if (error)
1438 goto out;
1439 idat = 0;
1440 if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
1441 error = ioctlf(fp, SEQUENCER_TMR_SOURCE, &idat);
1442 if (error)
1443 goto out;
1444 idat1 = idat;
1445 if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
1446 retval[0] = idat;
1447 break;
1448 case OSS_TMR_METRONOME:
1449 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1450 if (error)
1451 goto out;
1452 error = ioctlf(fp, SEQUENCER_TMR_METRONOME, &idat);
1453 goto out;
1454 case OSS_TMR_SELECT:
1455 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1456 if (error)
1457 goto out;
1458 retval[0] = idat;
1459 error = ioctlf(fp, SEQUENCER_TMR_SELECT, &idat);
1460 goto out;
1461 default:
1462 DPRINTF(("%s: Unknown sequencer command 0x%lx\n", __func__,
1463 com));
1464 error = EINVAL;
1465 goto out;
1468 error = copyout(&idat, SCARG(uap, data), sizeof idat);
1469 out:
1470 fd_putfile(SCARG(uap, fd));
1471 return error;
1475 * Check that the blocksize is a power of 2 as OSS wants.
1476 * If not, set it to be.
1478 static void
1479 setblocksize(file_t *fp, struct audio_info *info)
1481 struct audio_info set;
1482 int s;
1484 if (info->blocksize & (info->blocksize-1)) {
1485 for(s = 32; s < info->blocksize; s <<= 1)
1487 AUDIO_INITINFO(&set);
1488 set.blocksize = s;
1489 fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, &set);
1490 fp->f_ops->fo_ioctl(fp, AUDIO_GETBUFINFO, info);