Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libossaudio / ossaudio.c
blobc95da31c574d2d5b41385bca84c20ac10fa1a6e5
1 /* $NetBSD: ossaudio.c,v 1.23 2007/11/08 20:27:25 drochner Exp $ */
3 /*-
4 * Copyright (c) 1997 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 __RCSID("$NetBSD: ossaudio.c,v 1.23 2007/11/08 20:27:25 drochner Exp $");
33 * This is an OSS (Linux) sound API emulator.
34 * It provides the essentials of the API.
37 /* XXX This file is essentially the same as sys/compat/ossaudio.c.
38 * With some preprocessor magic it could be the same file.
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/audioio.h>
45 #include <sys/stat.h>
46 #include <errno.h>
48 #include "soundcard.h"
49 #undef ioctl
51 #define GET_DEV(com) ((com) & 0xff)
53 #define TO_OSSVOL(x) (((x) * 100 + 127) / 255)
54 #define FROM_OSSVOL(x) ((((x) > 100 ? 100 : (x)) * 255 + 50) / 100)
56 static struct audiodevinfo *getdevinfo(int);
58 static void setblocksize(int, struct audio_info *);
60 static int audio_ioctl(int, unsigned long, void *);
61 static int mixer_ioctl(int, unsigned long, void *);
62 static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq);
63 static int enum_to_ord(struct audiodevinfo *di, int enm);
64 static int enum_to_mask(struct audiodevinfo *di, int enm);
66 #define INTARG (*(int*)argp)
68 int
69 _oss_ioctl(int fd, unsigned long com, void *argp)
71 if (IOCGROUP(com) == 'P')
72 return audio_ioctl(fd, com, argp);
73 else if (IOCGROUP(com) == 'M')
74 return mixer_ioctl(fd, com, argp);
75 else
76 return ioctl(fd, com, argp);
79 static int
80 audio_ioctl(int fd, unsigned long com, void *argp)
83 struct audio_info tmpinfo;
84 struct audio_offset tmpoffs;
85 struct audio_buf_info bufinfo;
86 struct count_info cntinfo;
87 struct audio_encoding tmpenc;
88 u_int u;
89 int idat, idata;
90 int retval;
92 idat = 0;
94 switch (com) {
95 case SNDCTL_DSP_RESET:
96 retval = ioctl(fd, AUDIO_FLUSH, 0);
97 if (retval < 0)
98 return retval;
99 break;
100 case SNDCTL_DSP_SYNC:
101 retval = ioctl(fd, AUDIO_DRAIN, 0);
102 if (retval < 0)
103 return retval;
104 break;
105 case SNDCTL_DSP_POST:
106 /* This call is merely advisory, and may be a nop. */
107 break;
108 case SNDCTL_DSP_SPEED:
109 AUDIO_INITINFO(&tmpinfo);
110 tmpinfo.play.sample_rate =
111 tmpinfo.record.sample_rate = INTARG;
112 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
113 /* FALLTHRU */
114 case SOUND_PCM_READ_RATE:
115 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
116 if (retval < 0)
117 return retval;
118 INTARG = tmpinfo.play.sample_rate;
119 break;
120 case SNDCTL_DSP_STEREO:
121 AUDIO_INITINFO(&tmpinfo);
122 tmpinfo.play.channels =
123 tmpinfo.record.channels = INTARG ? 2 : 1;
124 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
125 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
126 if (retval < 0)
127 return retval;
128 INTARG = tmpinfo.play.channels - 1;
129 break;
130 case SNDCTL_DSP_GETBLKSIZE:
131 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
132 if (retval < 0)
133 return retval;
134 setblocksize(fd, &tmpinfo);
135 INTARG = tmpinfo.blocksize;
136 break;
137 case SNDCTL_DSP_SETFMT:
138 AUDIO_INITINFO(&tmpinfo);
139 switch (INTARG) {
140 case AFMT_MU_LAW:
141 tmpinfo.play.precision =
142 tmpinfo.record.precision = 8;
143 tmpinfo.play.encoding =
144 tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
145 break;
146 case AFMT_A_LAW:
147 tmpinfo.play.precision =
148 tmpinfo.record.precision = 8;
149 tmpinfo.play.encoding =
150 tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
151 break;
152 case AFMT_U8:
153 tmpinfo.play.precision =
154 tmpinfo.record.precision = 8;
155 tmpinfo.play.encoding =
156 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
157 break;
158 case AFMT_S8:
159 tmpinfo.play.precision =
160 tmpinfo.record.precision = 8;
161 tmpinfo.play.encoding =
162 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
163 break;
164 case AFMT_S16_LE:
165 tmpinfo.play.precision =
166 tmpinfo.record.precision = 16;
167 tmpinfo.play.encoding =
168 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
169 break;
170 case AFMT_S16_BE:
171 tmpinfo.play.precision =
172 tmpinfo.record.precision = 16;
173 tmpinfo.play.encoding =
174 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
175 break;
176 case AFMT_U16_LE:
177 tmpinfo.play.precision =
178 tmpinfo.record.precision = 16;
179 tmpinfo.play.encoding =
180 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
181 break;
182 case AFMT_U16_BE:
183 tmpinfo.play.precision =
184 tmpinfo.record.precision = 16;
185 tmpinfo.play.encoding =
186 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
187 break;
188 default:
189 return EINVAL;
191 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
192 /* FALLTHRU */
193 case SOUND_PCM_READ_BITS:
194 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
195 if (retval < 0)
196 return retval;
197 switch (tmpinfo.play.encoding) {
198 case AUDIO_ENCODING_ULAW:
199 idat = AFMT_MU_LAW;
200 break;
201 case AUDIO_ENCODING_ALAW:
202 idat = AFMT_A_LAW;
203 break;
204 case AUDIO_ENCODING_SLINEAR_LE:
205 if (tmpinfo.play.precision == 16)
206 idat = AFMT_S16_LE;
207 else
208 idat = AFMT_S8;
209 break;
210 case AUDIO_ENCODING_SLINEAR_BE:
211 if (tmpinfo.play.precision == 16)
212 idat = AFMT_S16_BE;
213 else
214 idat = AFMT_S8;
215 break;
216 case AUDIO_ENCODING_ULINEAR_LE:
217 if (tmpinfo.play.precision == 16)
218 idat = AFMT_U16_LE;
219 else
220 idat = AFMT_U8;
221 break;
222 case AUDIO_ENCODING_ULINEAR_BE:
223 if (tmpinfo.play.precision == 16)
224 idat = AFMT_U16_BE;
225 else
226 idat = AFMT_U8;
227 break;
228 case AUDIO_ENCODING_ADPCM:
229 idat = AFMT_IMA_ADPCM;
230 break;
232 INTARG = idat;
233 break;
234 case SNDCTL_DSP_CHANNELS:
235 AUDIO_INITINFO(&tmpinfo);
236 tmpinfo.play.channels = INTARG;
237 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
238 AUDIO_INITINFO(&tmpinfo);
239 tmpinfo.record.channels = INTARG;
240 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
241 /* FALLTHRU */
242 case SOUND_PCM_READ_CHANNELS:
243 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
244 if (retval < 0)
245 return retval;
246 INTARG = tmpinfo.play.channels;
247 break;
248 case SOUND_PCM_WRITE_FILTER:
249 case SOUND_PCM_READ_FILTER:
250 errno = EINVAL;
251 return -1; /* XXX unimplemented */
252 case SNDCTL_DSP_SUBDIVIDE:
253 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
254 if (retval < 0)
255 return retval;
256 setblocksize(fd, &tmpinfo);
257 idat = INTARG;
258 if (idat == 0)
259 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
260 idat = (tmpinfo.play.buffer_size / idat) & -4;
261 AUDIO_INITINFO(&tmpinfo);
262 tmpinfo.blocksize = idat;
263 retval = ioctl(fd, AUDIO_SETINFO, &tmpinfo);
264 if (retval < 0)
265 return retval;
266 INTARG = tmpinfo.play.buffer_size / tmpinfo.blocksize;
267 break;
268 case SNDCTL_DSP_SETFRAGMENT:
269 AUDIO_INITINFO(&tmpinfo);
270 idat = INTARG;
271 if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17)
272 return EINVAL;
273 tmpinfo.blocksize = 1 << (idat & 0xffff);
274 tmpinfo.hiwat = ((unsigned)idat >> 16) & 0x7fff;
275 if (tmpinfo.hiwat == 0) /* 0 means set to max */
276 tmpinfo.hiwat = 65536;
277 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
278 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
279 if (retval < 0)
280 return retval;
281 u = tmpinfo.blocksize;
282 for(idat = 0; u > 1; idat++, u >>= 1)
284 idat |= (tmpinfo.hiwat & 0x7fff) << 16;
285 INTARG = idat;
286 break;
287 case SNDCTL_DSP_GETFMTS:
288 for(idat = 0, tmpenc.index = 0;
289 ioctl(fd, AUDIO_GETENC, &tmpenc) == 0;
290 tmpenc.index++) {
291 switch(tmpenc.encoding) {
292 case AUDIO_ENCODING_ULAW:
293 idat |= AFMT_MU_LAW;
294 break;
295 case AUDIO_ENCODING_ALAW:
296 idat |= AFMT_A_LAW;
297 break;
298 case AUDIO_ENCODING_SLINEAR:
299 idat |= AFMT_S8;
300 break;
301 case AUDIO_ENCODING_SLINEAR_LE:
302 if (tmpenc.precision == 16)
303 idat |= AFMT_S16_LE;
304 else
305 idat |= AFMT_S8;
306 break;
307 case AUDIO_ENCODING_SLINEAR_BE:
308 if (tmpenc.precision == 16)
309 idat |= AFMT_S16_BE;
310 else
311 idat |= AFMT_S8;
312 break;
313 case AUDIO_ENCODING_ULINEAR:
314 idat |= AFMT_U8;
315 break;
316 case AUDIO_ENCODING_ULINEAR_LE:
317 if (tmpenc.precision == 16)
318 idat |= AFMT_U16_LE;
319 else
320 idat |= AFMT_U8;
321 break;
322 case AUDIO_ENCODING_ULINEAR_BE:
323 if (tmpenc.precision == 16)
324 idat |= AFMT_U16_BE;
325 else
326 idat |= AFMT_U8;
327 break;
328 case AUDIO_ENCODING_ADPCM:
329 idat |= AFMT_IMA_ADPCM;
330 break;
331 default:
332 break;
335 INTARG = idat;
336 break;
337 case SNDCTL_DSP_GETOSPACE:
338 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
339 if (retval < 0)
340 return retval;
341 setblocksize(fd, &tmpinfo);
342 bufinfo.fragsize = tmpinfo.blocksize;
343 bufinfo.fragments = tmpinfo.hiwat -
344 (tmpinfo.play.seek + tmpinfo.blocksize - 1)/tmpinfo.blocksize;
345 bufinfo.fragstotal = tmpinfo.hiwat;
346 bufinfo.bytes = tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
347 *(struct audio_buf_info *)argp = bufinfo;
348 break;
349 case SNDCTL_DSP_GETISPACE:
350 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
351 if (retval < 0)
352 return retval;
353 setblocksize(fd, &tmpinfo);
354 bufinfo.fragsize = tmpinfo.blocksize;
355 bufinfo.fragments = tmpinfo.hiwat -
356 (tmpinfo.record.seek + tmpinfo.blocksize - 1)/tmpinfo.blocksize;
357 bufinfo.fragstotal = tmpinfo.hiwat;
358 bufinfo.bytes = tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.record.seek;
359 *(struct audio_buf_info *)argp = bufinfo;
360 break;
361 case SNDCTL_DSP_NONBLOCK:
362 idat = 1;
363 retval = ioctl(fd, FIONBIO, &idat);
364 if (retval < 0)
365 return retval;
366 break;
367 case SNDCTL_DSP_GETCAPS:
368 retval = ioctl(fd, AUDIO_GETPROPS, &idata);
369 if (retval < 0)
370 return retval;
371 idat = DSP_CAP_TRIGGER; /* pretend we have trigger */
372 if (idata & AUDIO_PROP_FULLDUPLEX)
373 idat |= DSP_CAP_DUPLEX;
374 if (idata & AUDIO_PROP_MMAP)
375 idat |= DSP_CAP_MMAP;
376 INTARG = idat;
377 break;
378 #if 0
379 case SNDCTL_DSP_GETTRIGGER:
380 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
381 if (retval < 0)
382 return retval;
383 idat = (tmpinfo.play.pause ? 0 : PCM_ENABLE_OUTPUT) |
384 (tmpinfo.record.pause ? 0 : PCM_ENABLE_INPUT);
385 retval = copyout(&idat, SCARG(uap, data), sizeof idat);
386 if (retval < 0)
387 return retval;
388 break;
389 case SNDCTL_DSP_SETTRIGGER:
390 AUDIO_INITINFO(&tmpinfo);
391 retval = copyin(SCARG(uap, data), &idat, sizeof idat);
392 if (retval < 0)
393 return retval;
394 tmpinfo.play.pause = (idat & PCM_ENABLE_OUTPUT) == 0;
395 tmpinfo.record.pause = (idat & PCM_ENABLE_INPUT) == 0;
396 (void) ioctl(fd, AUDIO_SETINFO, &tmpinfo);
397 retval = copyout(&idat, SCARG(uap, data), sizeof idat);
398 if (retval < 0)
399 return retval;
400 break;
401 #else
402 case SNDCTL_DSP_GETTRIGGER:
403 case SNDCTL_DSP_SETTRIGGER:
404 /* XXX Do nothing for now. */
405 INTARG = PCM_ENABLE_OUTPUT;
406 break;
407 #endif
408 case SNDCTL_DSP_GETIPTR:
409 retval = ioctl(fd, AUDIO_GETIOFFS, &tmpoffs);
410 if (retval < 0)
411 return retval;
412 cntinfo.bytes = tmpoffs.samples;
413 cntinfo.blocks = tmpoffs.deltablks;
414 cntinfo.ptr = tmpoffs.offset;
415 *(struct count_info *)argp = cntinfo;
416 break;
417 case SNDCTL_DSP_GETOPTR:
418 retval = ioctl(fd, AUDIO_GETOOFFS, &tmpoffs);
419 if (retval < 0)
420 return retval;
421 cntinfo.bytes = tmpoffs.samples;
422 cntinfo.blocks = tmpoffs.deltablks;
423 cntinfo.ptr = tmpoffs.offset;
424 *(struct count_info *)argp = cntinfo;
425 break;
426 case SNDCTL_DSP_SETDUPLEX:
427 idat = 1;
428 retval = ioctl(fd, AUDIO_SETFD, &idat);
429 if (retval < 0)
430 return retval;
431 break;
432 case SNDCTL_DSP_GETODELAY:
433 retval = ioctl(fd, AUDIO_GETBUFINFO, &tmpinfo);
434 if (retval < 0)
435 return retval;
436 idat = tmpinfo.play.seek + tmpinfo.blocksize / 2;
437 INTARG = idat;
438 break;
439 case SNDCTL_DSP_PROFILE:
440 /* This gives just a hint to the driver,
441 * implementing it as a NOP is ok
443 break;
444 case SNDCTL_DSP_MAPINBUF:
445 case SNDCTL_DSP_MAPOUTBUF:
446 case SNDCTL_DSP_SETSYNCRO:
447 errno = EINVAL;
448 return -1; /* XXX unimplemented */
449 default:
450 errno = EINVAL;
451 return -1;
454 return 0;
458 /* If the NetBSD mixer device should have more than NETBSD_MAXDEVS devices
459 * some will not be available to Linux */
460 #define NETBSD_MAXDEVS 64
461 struct audiodevinfo {
462 int done;
463 dev_t dev;
464 int16_t devmap[SOUND_MIXER_NRDEVICES],
465 rdevmap[NETBSD_MAXDEVS];
466 char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
467 int enum2opaque[NETBSD_MAXDEVS];
468 u_long devmask, recmask, stereomask;
469 u_long caps;
470 int source;
473 static int
474 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
476 int i, o;
478 for (i = 0; i < NETBSD_MAXDEVS; i++) {
479 o = di->enum2opaque[i];
480 if (o == opq)
481 break;
482 if (o == -1 && label != NULL &&
483 !strncmp(di->names[i], label->name, sizeof di->names[i])) {
484 di->enum2opaque[i] = opq;
485 break;
488 if (i >= NETBSD_MAXDEVS)
489 i = -1;
490 /*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
491 return (i);
494 static int
495 enum_to_ord(struct audiodevinfo *di, int enm)
497 if (enm >= NETBSD_MAXDEVS)
498 return (-1);
500 /*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
501 return (di->enum2opaque[enm]);
504 static int
505 enum_to_mask(struct audiodevinfo *di, int enm)
507 int m;
508 if (enm >= NETBSD_MAXDEVS)
509 return (0);
511 m = di->enum2opaque[enm];
512 if (m == -1)
513 m = 0;
514 /*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
515 return (m);
519 * Collect the audio device information to allow faster
520 * emulation of the Linux mixer ioctls. Cache the information
521 * to eliminate the overhead of repeating all the ioctls needed
522 * to collect the information.
524 static struct audiodevinfo *
525 getdevinfo(int fd)
527 mixer_devinfo_t mi;
528 int i, j, e;
529 static struct {
530 char *name;
531 int code;
532 } *dp, devs[] = {
533 { AudioNmicrophone, SOUND_MIXER_MIC },
534 { AudioNline, SOUND_MIXER_LINE },
535 { AudioNcd, SOUND_MIXER_CD },
536 { AudioNdac, SOUND_MIXER_PCM },
537 { AudioNaux, SOUND_MIXER_LINE1 },
538 { AudioNrecord, SOUND_MIXER_IMIX },
539 { AudioNmaster, SOUND_MIXER_VOLUME },
540 { AudioNtreble, SOUND_MIXER_TREBLE },
541 { AudioNbass, SOUND_MIXER_BASS },
542 { AudioNspeaker, SOUND_MIXER_SPEAKER },
543 /* { AudioNheadphone, ?? },*/
544 { AudioNoutput, SOUND_MIXER_OGAIN },
545 { AudioNinput, SOUND_MIXER_IGAIN },
546 /* { AudioNmaster, SOUND_MIXER_SPEAKER },*/
547 /* { AudioNstereo, ?? },*/
548 /* { AudioNmono, ?? },*/
549 { AudioNfmsynth, SOUND_MIXER_SYNTH },
550 /* { AudioNwave, SOUND_MIXER_PCM },*/
551 { AudioNmidi, SOUND_MIXER_SYNTH },
552 /* { AudioNmixerout, ?? },*/
553 { 0, -1 }
555 static struct audiodevinfo devcache = { 0 };
556 struct audiodevinfo *di = &devcache;
557 struct stat sb;
558 int mlen, dlen;
560 /* Figure out what device it is so we can check if the
561 * cached data is valid.
563 if (fstat(fd, &sb) < 0)
564 return 0;
565 if (di->done && di->dev == sb.st_dev)
566 return di;
568 di->done = 1;
569 di->dev = sb.st_dev;
570 di->devmask = 0;
571 di->recmask = 0;
572 di->stereomask = 0;
573 di->source = ~0;
574 di->caps = 0;
575 for(i = 0; i < SOUND_MIXER_NRDEVICES; i++)
576 di->devmap[i] = -1;
577 for(i = 0; i < NETBSD_MAXDEVS; i++) {
578 di->rdevmap[i] = -1;
579 di->names[i][0] = '\0';
580 di->enum2opaque[i] = -1;
582 for(i = 0; i < NETBSD_MAXDEVS; i++) {
583 mi.index = i;
584 if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mi) < 0)
585 break;
586 switch(mi.type) {
587 case AUDIO_MIXER_VALUE:
588 for(dp = devs; dp->name; dp++) {
589 if (strcmp(dp->name, mi.label.name) == 0)
590 break;
591 dlen = strlen(dp->name);
592 mlen = strlen(mi.label.name);
593 if (dlen < mlen
594 && mi.label.name[mlen-dlen-1] == '.'
595 && strcmp(dp->name, mi.label.name + mlen - dlen) == 0)
596 break;
598 if (dp->code >= 0) {
599 di->devmap[dp->code] = i;
600 di->rdevmap[i] = dp->code;
601 di->devmask |= 1 << dp->code;
602 if (mi.un.v.num_channels == 2)
603 di->stereomask |= 1 << dp->code;
604 strncpy(di->names[i], mi.label.name,
605 sizeof di->names[i]);
607 break;
610 for(i = 0; i < NETBSD_MAXDEVS; i++) {
611 mi.index = i;
612 if (ioctl(fd, AUDIO_MIXER_DEVINFO, &mi) < 0)
613 break;
614 if (strcmp(mi.label.name, AudioNsource) != 0)
615 continue;
616 di->source = i;
617 switch(mi.type) {
618 case AUDIO_MIXER_ENUM:
619 for(j = 0; j < mi.un.e.num_mem; j++) {
620 e = opaque_to_enum(di,
621 &mi.un.e.member[j].label,
622 mi.un.e.member[j].ord);
623 if (e >= 0)
624 di->recmask |= 1 << di->rdevmap[e];
626 di->caps = SOUND_CAP_EXCL_INPUT;
627 break;
628 case AUDIO_MIXER_SET:
629 for(j = 0; j < mi.un.s.num_mem; j++) {
630 e = opaque_to_enum(di,
631 &mi.un.s.member[j].label,
632 mi.un.s.member[j].mask);
633 if (e >= 0)
634 di->recmask |= 1 << di->rdevmap[e];
636 break;
639 return di;
643 mixer_ioctl(int fd, unsigned long com, void *argp)
645 struct audiodevinfo *di;
646 struct mixer_info *omi;
647 struct audio_device adev;
648 mixer_ctrl_t mc;
649 int idat;
650 int i;
651 int retval;
652 int l, r, n, error, e;
654 idat = 0;
655 di = getdevinfo(fd);
656 if (di == 0)
657 return -1;
659 switch (com) {
660 case OSS_GETVERSION:
661 idat = SOUND_VERSION;
662 break;
663 case SOUND_MIXER_INFO:
664 case SOUND_OLD_MIXER_INFO:
665 error = ioctl(fd, AUDIO_GETDEV, &adev);
666 if (error)
667 return (error);
668 omi = argp;
669 if (com == SOUND_MIXER_INFO)
670 omi->modify_counter = 1;
671 strncpy(omi->id, adev.name, sizeof omi->id);
672 strncpy(omi->name, adev.name, sizeof omi->name);
673 return 0;
674 case SOUND_MIXER_READ_RECSRC:
675 if (di->source == -1)
676 return EINVAL;
677 mc.dev = di->source;
678 if (di->caps & SOUND_CAP_EXCL_INPUT) {
679 mc.type = AUDIO_MIXER_ENUM;
680 retval = ioctl(fd, AUDIO_MIXER_READ, &mc);
681 if (retval < 0)
682 return retval;
683 e = opaque_to_enum(di, NULL, mc.un.ord);
684 if (e >= 0)
685 idat = 1 << di->rdevmap[e];
686 } else {
687 mc.type = AUDIO_MIXER_SET;
688 retval = ioctl(fd, AUDIO_MIXER_READ, &mc);
689 if (retval < 0)
690 return retval;
691 e = opaque_to_enum(di, NULL, mc.un.mask);
692 if (e >= 0)
693 idat = 1 << di->rdevmap[e];
695 break;
696 case SOUND_MIXER_READ_DEVMASK:
697 idat = di->devmask;
698 break;
699 case SOUND_MIXER_READ_RECMASK:
700 idat = di->recmask;
701 break;
702 case SOUND_MIXER_READ_STEREODEVS:
703 idat = di->stereomask;
704 break;
705 case SOUND_MIXER_READ_CAPS:
706 idat = di->caps;
707 break;
708 case SOUND_MIXER_WRITE_RECSRC:
709 case SOUND_MIXER_WRITE_R_RECSRC:
710 if (di->source == -1)
711 return EINVAL;
712 mc.dev = di->source;
713 idat = INTARG;
714 if (di->caps & SOUND_CAP_EXCL_INPUT) {
715 mc.type = AUDIO_MIXER_ENUM;
716 for(i = 0; i < SOUND_MIXER_NRDEVICES; i++)
717 if (idat & (1 << i))
718 break;
719 if (i >= SOUND_MIXER_NRDEVICES ||
720 di->devmap[i] == -1)
721 return EINVAL;
722 mc.un.ord = enum_to_ord(di, di->devmap[i]);
723 } else {
724 mc.type = AUDIO_MIXER_SET;
725 mc.un.mask = 0;
726 for(i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
727 if (idat & (1 << i)) {
728 if (di->devmap[i] == -1)
729 return EINVAL;
730 mc.un.mask |= enum_to_mask(di, di->devmap[i]);
734 return ioctl(fd, AUDIO_MIXER_WRITE, &mc);
735 default:
736 if (MIXER_READ(SOUND_MIXER_FIRST) <= com &&
737 com < MIXER_READ(SOUND_MIXER_NRDEVICES)) {
738 n = GET_DEV(com);
739 if (di->devmap[n] == -1)
740 return EINVAL;
741 mc.dev = di->devmap[n];
742 mc.type = AUDIO_MIXER_VALUE;
743 doread:
744 mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
745 retval = ioctl(fd, AUDIO_MIXER_READ, &mc);
746 if (retval < 0)
747 return retval;
748 if (mc.type != AUDIO_MIXER_VALUE)
749 return EINVAL;
750 if (mc.un.value.num_channels != 2) {
751 l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
752 } else {
753 l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
754 r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
756 idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
757 break;
758 } else if ((MIXER_WRITE_R(SOUND_MIXER_FIRST) <= com &&
759 com < MIXER_WRITE_R(SOUND_MIXER_NRDEVICES)) ||
760 (MIXER_WRITE(SOUND_MIXER_FIRST) <= com &&
761 com < MIXER_WRITE(SOUND_MIXER_NRDEVICES))) {
762 n = GET_DEV(com);
763 if (di->devmap[n] == -1)
764 return EINVAL;
765 idat = INTARG;
766 l = FROM_OSSVOL( idat & 0xff);
767 r = FROM_OSSVOL((idat >> 8) & 0xff);
768 mc.dev = di->devmap[n];
769 mc.type = AUDIO_MIXER_VALUE;
770 if (di->stereomask & (1<<n)) {
771 mc.un.value.num_channels = 2;
772 mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
773 mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
774 } else {
775 mc.un.value.num_channels = 1;
776 mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
778 retval = ioctl(fd, AUDIO_MIXER_WRITE, &mc);
779 if (retval < 0)
780 return retval;
781 if (MIXER_WRITE(SOUND_MIXER_FIRST) <= com &&
782 com < MIXER_WRITE(SOUND_MIXER_NRDEVICES))
783 return 0;
784 goto doread;
785 } else {
786 errno = EINVAL;
787 return -1;
790 INTARG = idat;
791 return 0;
795 * Check that the blocksize is a power of 2 as OSS wants.
796 * If not, set it to be.
798 static void
799 setblocksize(int fd, struct audio_info *info)
801 struct audio_info set;
802 int s;
804 if (info->blocksize & (info->blocksize-1)) {
805 for(s = 32; s < info->blocksize; s <<= 1)
807 AUDIO_INITINFO(&set);
808 set.blocksize = s;
809 ioctl(fd, AUDIO_SETINFO, &set);
810 ioctl(fd, AUDIO_GETBUFINFO, info);