ossaudio.c revision 1.36 1 /* $NetBSD: ossaudio.c,v 1.36 2001/06/14 20:32:44 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/file.h>
40 #include <sys/vnode.h>
41 #include <sys/filedesc.h>
42 #include <sys/ioctl.h>
43 #include <sys/mount.h>
44 #include <sys/kernel.h>
45 #include <sys/audioio.h>
46 #include <sys/midiio.h>
47
48 #include <sys/syscallargs.h>
49
50 #include <compat/ossaudio/ossaudio.h>
51 #include <compat/ossaudio/ossaudiovar.h>
52
53 #ifdef AUDIO_DEBUG
54 #define DPRINTF(x) if (ossdebug) printf x
55 int ossdebug = 0;
56 #else
57 #define DPRINTF(x)
58 #endif
59
60 #define TO_OSSVOL(x) (((x) * 100 + 127) / 255)
61 #define FROM_OSSVOL(x) ((((x) > 100 ? 100 : (x)) * 255 + 50) / 100)
62
63 static struct audiodevinfo *getdevinfo __P((struct file *, struct proc *));
64 static int opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq);
65 static int enum_to_ord(struct audiodevinfo *di, int enm);
66 static int enum_to_mask(struct audiodevinfo *di, int enm);
67
68 static void setblocksize __P((struct file *, struct audio_info *, struct proc *));
69
70
71 int
72 oss_ioctl_audio(p, uap, retval)
73 struct proc *p;
74 struct oss_sys_ioctl_args /* {
75 syscallarg(int) fd;
76 syscallarg(u_long) com;
77 syscallarg(caddr_t) data;
78 } */ *uap;
79 register_t *retval;
80 {
81 struct file *fp;
82 struct filedesc *fdp;
83 u_long com;
84 struct audio_info tmpinfo;
85 struct audio_offset tmpoffs;
86 struct oss_audio_buf_info bufinfo;
87 struct oss_count_info cntinfo;
88 struct audio_encoding tmpenc;
89 u_int u;
90 int idat, idata;
91 int error = 0;
92 int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
93
94 fdp = p->p_fd;
95 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
96 return (EBADF);
97
98 FILE_USE(fp);
99
100 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
101 error = EBADF;
102 goto out;
103 }
104
105 com = SCARG(uap, com);
106 DPRINTF(("oss_ioctl_audio: com=%08lx\n", com));
107
108 retval[0] = 0;
109
110 ioctlf = fp->f_ops->fo_ioctl;
111 switch (com) {
112 case OSS_SNDCTL_DSP_RESET:
113 error = ioctlf(fp, AUDIO_FLUSH, (caddr_t)0, p);
114 if (error)
115 goto out;
116 break;
117 case OSS_SNDCTL_DSP_SYNC:
118 case OSS_SNDCTL_DSP_POST:
119 error = ioctlf(fp, AUDIO_DRAIN, (caddr_t)0, p);
120 if (error)
121 goto out;
122 break;
123 case OSS_SNDCTL_DSP_SPEED:
124 AUDIO_INITINFO(&tmpinfo);
125 error = copyin(SCARG(uap, data), &idat, sizeof idat);
126 if (error)
127 goto out;
128 tmpinfo.play.sample_rate =
129 tmpinfo.record.sample_rate = idat;
130 error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
131 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_SPEED %d = %d\n",
132 idat, error));
133 if (error)
134 goto out;
135 /* fall into ... */
136 case OSS_SOUND_PCM_READ_RATE:
137 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
138 if (error)
139 goto out;
140 idat = tmpinfo.play.sample_rate;
141 error = copyout(&idat, SCARG(uap, data), sizeof idat);
142 if (error)
143 goto out;
144 break;
145 case OSS_SNDCTL_DSP_STEREO:
146 AUDIO_INITINFO(&tmpinfo);
147 error = copyin(SCARG(uap, data), &idat, sizeof idat);
148 if (error)
149 goto out;
150 tmpinfo.play.channels =
151 tmpinfo.record.channels = idat ? 2 : 1;
152 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
153 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
154 if (error)
155 goto out;
156 idat = tmpinfo.play.channels - 1;
157 error = copyout(&idat, SCARG(uap, data), sizeof idat);
158 if (error)
159 goto out;
160 break;
161 case OSS_SNDCTL_DSP_GETBLKSIZE:
162 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
163 if (error)
164 goto out;
165 setblocksize(fp, &tmpinfo, p);
166 idat = tmpinfo.blocksize;
167 error = copyout(&idat, SCARG(uap, data), sizeof idat);
168 if (error)
169 goto out;
170 break;
171 case OSS_SNDCTL_DSP_SETFMT:
172 AUDIO_INITINFO(&tmpinfo);
173 error = copyin(SCARG(uap, data), &idat, sizeof idat);
174 if (error)
175 goto out;
176 switch (idat) {
177 case OSS_AFMT_MU_LAW:
178 tmpinfo.play.precision =
179 tmpinfo.record.precision = 8;
180 tmpinfo.play.encoding =
181 tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
182 break;
183 case OSS_AFMT_A_LAW:
184 tmpinfo.play.precision =
185 tmpinfo.record.precision = 8;
186 tmpinfo.play.encoding =
187 tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
188 break;
189 case OSS_AFMT_U8:
190 tmpinfo.play.precision =
191 tmpinfo.record.precision = 8;
192 tmpinfo.play.encoding =
193 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
194 break;
195 case OSS_AFMT_S8:
196 tmpinfo.play.precision =
197 tmpinfo.record.precision = 8;
198 tmpinfo.play.encoding =
199 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
200 break;
201 case OSS_AFMT_S16_LE:
202 tmpinfo.play.precision =
203 tmpinfo.record.precision = 16;
204 tmpinfo.play.encoding =
205 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
206 break;
207 case OSS_AFMT_S16_BE:
208 tmpinfo.play.precision =
209 tmpinfo.record.precision = 16;
210 tmpinfo.play.encoding =
211 tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
212 break;
213 case OSS_AFMT_U16_LE:
214 tmpinfo.play.precision =
215 tmpinfo.record.precision = 16;
216 tmpinfo.play.encoding =
217 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
218 break;
219 case OSS_AFMT_U16_BE:
220 tmpinfo.play.precision =
221 tmpinfo.record.precision = 16;
222 tmpinfo.play.encoding =
223 tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
224 break;
225 default:
226 error = EINVAL;
227 goto out;
228 }
229 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
230 /* fall into ... */
231 case OSS_SOUND_PCM_READ_BITS:
232 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
233 if (error)
234 goto out;
235 switch (tmpinfo.play.encoding) {
236 case AUDIO_ENCODING_ULAW:
237 idat = OSS_AFMT_MU_LAW;
238 break;
239 case AUDIO_ENCODING_ALAW:
240 idat = OSS_AFMT_A_LAW;
241 break;
242 case AUDIO_ENCODING_SLINEAR_LE:
243 if (tmpinfo.play.precision == 16)
244 idat = OSS_AFMT_S16_LE;
245 else
246 idat = OSS_AFMT_S8;
247 break;
248 case AUDIO_ENCODING_SLINEAR_BE:
249 if (tmpinfo.play.precision == 16)
250 idat = OSS_AFMT_S16_BE;
251 else
252 idat = OSS_AFMT_S8;
253 break;
254 case AUDIO_ENCODING_ULINEAR_LE:
255 if (tmpinfo.play.precision == 16)
256 idat = OSS_AFMT_U16_LE;
257 else
258 idat = OSS_AFMT_U8;
259 break;
260 case AUDIO_ENCODING_ULINEAR_BE:
261 if (tmpinfo.play.precision == 16)
262 idat = OSS_AFMT_U16_BE;
263 else
264 idat = OSS_AFMT_U8;
265 break;
266 case AUDIO_ENCODING_ADPCM:
267 idat = OSS_AFMT_IMA_ADPCM;
268 break;
269 }
270 error = copyout(&idat, SCARG(uap, data), sizeof idat);
271 if (error)
272 goto out;
273 break;
274 case OSS_SNDCTL_DSP_CHANNELS:
275 AUDIO_INITINFO(&tmpinfo);
276 error = copyin(SCARG(uap, data), &idat, sizeof idat);
277 if (error)
278 goto out;
279 tmpinfo.play.channels =
280 tmpinfo.record.channels = idat;
281 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
282 /* fall into ... */
283 case OSS_SOUND_PCM_READ_CHANNELS:
284 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
285 if (error)
286 goto out;
287 idat = tmpinfo.play.channels;
288 error = copyout(&idat, SCARG(uap, data), sizeof idat);
289 if (error)
290 goto out;
291 break;
292 case OSS_SOUND_PCM_WRITE_FILTER:
293 case OSS_SOUND_PCM_READ_FILTER:
294 error = EINVAL; /* XXX unimplemented */
295 goto out;
296 case OSS_SNDCTL_DSP_SUBDIVIDE:
297 error = copyin(SCARG(uap, data), &idat, sizeof idat);
298 if (error)
299 goto out;
300 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
301 setblocksize(fp, &tmpinfo, p);
302 if (error)
303 goto out;
304 if (idat == 0)
305 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
306 idat = (tmpinfo.play.buffer_size / idat) & -4;
307 AUDIO_INITINFO(&tmpinfo);
308 tmpinfo.blocksize = idat;
309 error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
310 if (error)
311 goto out;
312 idat = tmpinfo.play.buffer_size / tmpinfo.blocksize;
313 error = copyout(&idat, SCARG(uap, data), sizeof idat);
314 if (error)
315 goto out;
316 break;
317 case OSS_SNDCTL_DSP_SETFRAGMENT:
318 AUDIO_INITINFO(&tmpinfo);
319 error = copyin(SCARG(uap, data), &idat, sizeof idat);
320 if (error)
321 goto out;
322 if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17) {
323 error = EINVAL;
324 goto out;
325 }
326 tmpinfo.blocksize = 1 << (idat & 0xffff);
327 tmpinfo.hiwat = (idat >> 16) & 0x7fff;
328 DPRINTF(("oss_audio: SETFRAGMENT blksize=%d, hiwat=%d\n",
329 tmpinfo.blocksize, tmpinfo.hiwat));
330 if (tmpinfo.hiwat == 0) /* 0 means set to max */
331 tmpinfo.hiwat = 65536;
332 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
333 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
334 if (error)
335 goto out;
336 u = tmpinfo.blocksize;
337 for(idat = 0; u > 1; idat++, u >>= 1)
338 ;
339 idat |= (tmpinfo.hiwat & 0x7fff) << 16;
340 error = copyout(&idat, SCARG(uap, data), sizeof idat);
341 if (error)
342 goto out;
343 break;
344 case OSS_SNDCTL_DSP_GETFMTS:
345 for(idat = 0, tmpenc.index = 0;
346 ioctlf(fp, AUDIO_GETENC, (caddr_t)&tmpenc, p) == 0;
347 tmpenc.index++) {
348 switch(tmpenc.encoding) {
349 case AUDIO_ENCODING_ULAW:
350 idat |= OSS_AFMT_MU_LAW;
351 break;
352 case AUDIO_ENCODING_ALAW:
353 idat |= OSS_AFMT_A_LAW;
354 break;
355 case AUDIO_ENCODING_SLINEAR:
356 idat |= OSS_AFMT_S8;
357 break;
358 case AUDIO_ENCODING_SLINEAR_LE:
359 if (tmpenc.precision == 16)
360 idat |= OSS_AFMT_S16_LE;
361 else
362 idat |= OSS_AFMT_S8;
363 break;
364 case AUDIO_ENCODING_SLINEAR_BE:
365 if (tmpenc.precision == 16)
366 idat |= OSS_AFMT_S16_BE;
367 else
368 idat |= OSS_AFMT_S8;
369 break;
370 case AUDIO_ENCODING_ULINEAR:
371 idat |= OSS_AFMT_U8;
372 break;
373 case AUDIO_ENCODING_ULINEAR_LE:
374 if (tmpenc.precision == 16)
375 idat |= OSS_AFMT_U16_LE;
376 else
377 idat |= OSS_AFMT_U8;
378 break;
379 case AUDIO_ENCODING_ULINEAR_BE:
380 if (tmpenc.precision == 16)
381 idat |= OSS_AFMT_U16_BE;
382 else
383 idat |= OSS_AFMT_U8;
384 break;
385 case AUDIO_ENCODING_ADPCM:
386 idat |= OSS_AFMT_IMA_ADPCM;
387 break;
388 default:
389 break;
390 }
391 }
392 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETFMTS = %x\n", idat));
393 error = copyout(&idat, SCARG(uap, data), sizeof idat);
394 if (error)
395 goto out;
396 break;
397 case OSS_SNDCTL_DSP_GETOSPACE:
398 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
399 if (error)
400 goto out;
401 setblocksize(fp, &tmpinfo, p);
402 bufinfo.fragsize = tmpinfo.blocksize;
403 bufinfo.fragments = tmpinfo.hiwat -
404 (tmpinfo.play.seek + tmpinfo.blocksize - 1) /
405 tmpinfo.blocksize;
406 bufinfo.fragstotal = tmpinfo.hiwat;
407 bufinfo.bytes =
408 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.play.seek;
409 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
410 if (error)
411 goto out;
412 break;
413 case OSS_SNDCTL_DSP_GETISPACE:
414 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
415 if (error)
416 goto out;
417 setblocksize(fp, &tmpinfo, p);
418 bufinfo.fragsize = tmpinfo.blocksize;
419 bufinfo.fragments = tmpinfo.hiwat -
420 (tmpinfo.record.seek + tmpinfo.blocksize - 1) /
421 tmpinfo.blocksize;
422 bufinfo.fragstotal = tmpinfo.hiwat;
423 bufinfo.bytes =
424 tmpinfo.hiwat * tmpinfo.blocksize - tmpinfo.record.seek;
425 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETxSPACE = %d %d %d %d\n",
426 bufinfo.fragsize, bufinfo.fragments,
427 bufinfo.fragstotal, bufinfo.bytes));
428 error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
429 if (error)
430 goto out;
431 break;
432 case OSS_SNDCTL_DSP_NONBLOCK:
433 idat = 1;
434 error = ioctlf(fp, FIONBIO, (caddr_t)&idat, p);
435 if (error)
436 goto out;
437 break;
438 case OSS_SNDCTL_DSP_GETCAPS:
439 error = ioctlf(fp, AUDIO_GETPROPS, (caddr_t)&idata, p);
440 if (error)
441 goto out;
442 idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
443 if (idata & AUDIO_PROP_FULLDUPLEX)
444 idat |= OSS_DSP_CAP_DUPLEX;
445 if (idata & AUDIO_PROP_MMAP)
446 idat |= OSS_DSP_CAP_MMAP;
447 DPRINTF(("oss_sys_ioctl: SNDCTL_DSP_GETCAPS = %x\n", idat));
448 error = copyout(&idat, SCARG(uap, data), sizeof idat);
449 if (error)
450 goto out;
451 break;
452 #if 0
453 case OSS_SNDCTL_DSP_GETTRIGGER:
454 error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
455 if (error)
456 goto out;
457 idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
458 (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
459 error = copyout(&idat, SCARG(uap, data), sizeof idat);
460 if (error)
461 goto out;
462 break;
463 case OSS_SNDCTL_DSP_SETTRIGGER:
464 AUDIO_INITINFO(&tmpinfo);
465 error = copyin(SCARG(uap, data), &idat, sizeof idat);
466 if (error)
467 goto out;
468 tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
469 tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
470 (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
471 error = copyout(&idat, SCARG(uap, data), sizeof idat);
472 if (error)
473 goto out;
474 break;
475 #else
476 case OSS_SNDCTL_DSP_GETTRIGGER:
477 case OSS_SNDCTL_DSP_SETTRIGGER:
478 /* XXX Do nothing for now. */
479 idat = OSS_PCM_ENABLE_OUTPUT;
480 error = copyout(&idat, SCARG(uap, data), sizeof idat);
481 goto out;
482 #endif
483 case OSS_SNDCTL_DSP_GETIPTR:
484 error = ioctlf(fp, AUDIO_GETIOFFS, (caddr_t)&tmpoffs, p);
485 if (error)
486 goto out;
487 cntinfo.bytes = tmpoffs.samples;
488 cntinfo.blocks = tmpoffs.deltablks;
489 cntinfo.ptr = tmpoffs.offset;
490 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
491 if (error)
492 goto out;
493 break;
494 case OSS_SNDCTL_DSP_GETOPTR:
495 error = ioctlf(fp, AUDIO_GETOOFFS, (caddr_t)&tmpoffs, p);
496 if (error)
497 goto out;
498 cntinfo.bytes = tmpoffs.samples;
499 cntinfo.blocks = tmpoffs.deltablks;
500 cntinfo.ptr = tmpoffs.offset;
501 error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
502 if (error)
503 goto out;
504 break;
505 case OSS_SNDCTL_DSP_MAPINBUF:
506 case OSS_SNDCTL_DSP_MAPOUTBUF:
507 case OSS_SNDCTL_DSP_SETSYNCRO:
508 case OSS_SNDCTL_DSP_SETDUPLEX:
509 case OSS_SNDCTL_DSP_PROFILE:
510 error = EINVAL;
511 goto out;
512 default:
513 error = EINVAL;
514 goto out;
515 }
516
517 out:
518 FILE_UNUSE(fp, p);
519 return error;
520 }
521
522 /* If the NetBSD mixer device should have more than 32 devices
523 * some will not be available to Linux */
524 #define NETBSD_MAXDEVS 64
525 struct audiodevinfo {
526 int done;
527 dev_t dev;
528 int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
529 rdevmap[NETBSD_MAXDEVS];
530 char names[NETBSD_MAXDEVS][MAX_AUDIO_DEV_LEN];
531 int enum2opaque[NETBSD_MAXDEVS];
532 u_long devmask, recmask, stereomask;
533 u_long caps, source;
534 };
535
536 static int
537 opaque_to_enum(struct audiodevinfo *di, audio_mixer_name_t *label, int opq)
538 {
539 int i, o;
540
541 for (i = 0; i < NETBSD_MAXDEVS; i++) {
542 o = di->enum2opaque[i];
543 if (o == opq)
544 break;
545 if (o == -1 && label != NULL &&
546 !strncmp(di->names[i], label->name, sizeof di->names[i])) {
547 di->enum2opaque[i] = opq;
548 break;
549 }
550 }
551 if (i >= NETBSD_MAXDEVS)
552 i = -1;
553 /*printf("opq_to_enum %s %d -> %d\n", label->name, opq, i);*/
554 return (i);
555 }
556
557 static int
558 enum_to_ord(struct audiodevinfo *di, int enm)
559 {
560 if (enm >= NETBSD_MAXDEVS)
561 return (-1);
562
563 /*printf("enum_to_ord %d -> %d\n", enm, di->enum2opaque[enm]);*/
564 return (di->enum2opaque[enm]);
565 }
566
567 static int
568 enum_to_mask(struct audiodevinfo *di, int enm)
569 {
570 int m;
571 if (enm >= NETBSD_MAXDEVS)
572 return (0);
573
574 m = di->enum2opaque[enm];
575 if (m == -1)
576 m = 0;
577 /*printf("enum_to_mask %d -> %d\n", enm, di->enum2opaque[enm]);*/
578 return (m);
579 }
580
581 /*
582 * Collect the audio device information to allow faster
583 * emulation of the Linux mixer ioctls. Cache the information
584 * to eliminate the overhead of repeating all the ioctls needed
585 * to collect the information.
586 */
587 static struct audiodevinfo *
588 getdevinfo(fp, p)
589 struct file *fp;
590 struct proc *p;
591 {
592 mixer_devinfo_t mi;
593 int i, j, e;
594 static const struct {
595 const char *name;
596 int code;
597 } *dp, devs[] = {
598 { AudioNmicrophone, OSS_SOUND_MIXER_MIC },
599 { AudioNline, OSS_SOUND_MIXER_LINE },
600 { AudioNcd, OSS_SOUND_MIXER_CD },
601 { AudioNdac, OSS_SOUND_MIXER_PCM },
602 { AudioNrecord, OSS_SOUND_MIXER_IMIX },
603 { AudioNmaster, OSS_SOUND_MIXER_VOLUME },
604 { AudioNtreble, OSS_SOUND_MIXER_TREBLE },
605 { AudioNbass, OSS_SOUND_MIXER_BASS },
606 { AudioNspeaker, OSS_SOUND_MIXER_SPEAKER },
607 /* { AudioNheadphone, ?? },*/
608 { AudioNoutput, OSS_SOUND_MIXER_OGAIN },
609 { AudioNinput, OSS_SOUND_MIXER_IGAIN },
610 /* { AudioNmaster, OSS_SOUND_MIXER_SPEAKER },*/
611 /* { AudioNstereo, ?? },*/
612 /* { AudioNmono, ?? },*/
613 { AudioNfmsynth, OSS_SOUND_MIXER_SYNTH },
614 /* { AudioNwave, OSS_SOUND_MIXER_PCM },*/
615 { AudioNmidi, OSS_SOUND_MIXER_SYNTH },
616 /* { AudioNmixerout, ?? },*/
617 { 0, -1 }
618 };
619 int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *)) =
620 fp->f_ops->fo_ioctl;
621 struct vnode *vp;
622 struct vattr va;
623 static struct audiodevinfo devcache = { 0 };
624 struct audiodevinfo *di = &devcache;
625
626 /*
627 * Figure out what device it is so we can check if the
628 * cached data is valid.
629 */
630 vp = (struct vnode *)fp->f_data;
631 if (vp->v_type != VCHR)
632 return 0;
633 if (VOP_GETATTR(vp, &va, p->p_ucred, p))
634 return 0;
635 if (di->done && di->dev == va.va_rdev)
636 return di;
637
638 di->done = 1;
639 di->dev = va.va_rdev;
640 di->devmask = 0;
641 di->recmask = 0;
642 di->stereomask = 0;
643 di->source = ~0;
644 di->caps = 0;
645 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
646 di->devmap[i] = -1;
647 for(i = 0; i < NETBSD_MAXDEVS; i++) {
648 di->rdevmap[i] = -1;
649 di->names[i][0] = '\0';
650 di->enum2opaque[i] = -1;
651 }
652 for(i = 0; i < NETBSD_MAXDEVS; i++) {
653 mi.index = i;
654 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0)
655 break;
656 switch(mi.type) {
657 case AUDIO_MIXER_VALUE:
658 for(dp = devs; dp->name; dp++)
659 if (strcmp(dp->name, mi.label.name) == 0)
660 break;
661 if (dp->code >= 0) {
662 di->devmap[dp->code] = i;
663 di->rdevmap[i] = dp->code;
664 di->devmask |= 1 << dp->code;
665 if (mi.un.v.num_channels == 2)
666 di->stereomask |= 1 << dp->code;
667 strncpy(di->names[i], mi.label.name,
668 sizeof di->names[i]);
669 }
670 break;
671 }
672 }
673 for(i = 0; i < NETBSD_MAXDEVS; i++) {
674 mi.index = i;
675 if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0)
676 break;
677 if (strcmp(mi.label.name, AudioNsource) != 0)
678 continue;
679 di->source = i;
680 switch(mi.type) {
681 case AUDIO_MIXER_ENUM:
682 for(j = 0; j < mi.un.e.num_mem; j++) {
683 e = opaque_to_enum(di,
684 &mi.un.e.member[j].label,
685 mi.un.e.member[j].ord);
686 if (e >= 0)
687 di->recmask |= 1 << di->rdevmap[e];
688 }
689 di->caps = OSS_SOUND_CAP_EXCL_INPUT;
690 break;
691 case AUDIO_MIXER_SET:
692 for(j = 0; j < mi.un.s.num_mem; j++) {
693 e = opaque_to_enum(di,
694 &mi.un.s.member[j].label,
695 mi.un.s.member[j].mask);
696 if (e >= 0)
697 di->recmask |= 1 << di->rdevmap[e];
698 }
699 break;
700 }
701 }
702 return di;
703 }
704
705 int
706 oss_ioctl_mixer(p, uap, retval)
707 struct proc *p;
708 struct oss_sys_ioctl_args /* {
709 syscallarg(int) fd;
710 syscallarg(u_long) com;
711 syscallarg(caddr_t) data;
712 } */ *uap;
713 register_t *retval;
714 {
715 struct file *fp;
716 struct filedesc *fdp;
717 u_long com;
718 struct audiodevinfo *di;
719 mixer_ctrl_t mc;
720 struct oss_mixer_info omi;
721 struct audio_device adev;
722 int idat;
723 int i;
724 int error;
725 int l, r, n, e;
726 int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
727
728 fdp = p->p_fd;
729 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
730 return (EBADF);
731
732 FILE_USE(fp);
733
734 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
735 error = EBADF;
736 goto out;
737 }
738
739 com = SCARG(uap, com);
740 DPRINTF(("oss_ioctl_mixer: com=%08lx\n", com));
741
742 retval[0] = 0;
743
744 di = getdevinfo(fp, p);
745 if (di == 0) {
746 error = EINVAL;
747 goto out;
748 }
749
750 ioctlf = fp->f_ops->fo_ioctl;
751 switch (com) {
752 case OSS_GET_VERSION:
753 idat = OSS_SOUND_VERSION;
754 break;
755 case OSS_SOUND_MIXER_INFO:
756 case OSS_SOUND_OLD_MIXER_INFO:
757 error = ioctlf(fp, AUDIO_GETDEV, (caddr_t)&adev, p);
758 if (error)
759 return (error);
760 omi.modify_counter = 1;
761 strncpy(omi.id, adev.name, sizeof omi.id);
762 strncpy(omi.name, adev.name, sizeof omi.name);
763 return copyout(&omi, SCARG(uap, data), OSS_IOCTL_SIZE(com));
764 case OSS_SOUND_MIXER_READ_RECSRC:
765 if (di->source == -1) {
766 error = EINVAL;
767 goto out;
768 }
769 mc.dev = di->source;
770 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
771 mc.type = AUDIO_MIXER_ENUM;
772 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
773 if (error)
774 goto out;
775 e = opaque_to_enum(di, NULL, mc.un.ord);
776 if (e >= 0)
777 idat = 1 << di->rdevmap[e];
778 } else {
779 mc.type = AUDIO_MIXER_SET;
780 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
781 if (error)
782 goto out;
783 e = opaque_to_enum(di, NULL, mc.un.mask);
784 if (e >= 0)
785 idat = 1 << di->rdevmap[e];
786 }
787 break;
788 case OSS_SOUND_MIXER_READ_DEVMASK:
789 idat = di->devmask;
790 break;
791 case OSS_SOUND_MIXER_READ_RECMASK:
792 idat = di->recmask;
793 break;
794 case OSS_SOUND_MIXER_READ_STEREODEVS:
795 idat = di->stereomask;
796 break;
797 case OSS_SOUND_MIXER_READ_CAPS:
798 idat = di->caps;
799 break;
800 case OSS_SOUND_MIXER_WRITE_RECSRC:
801 case OSS_SOUND_MIXER_WRITE_R_RECSRC:
802 if (di->source == -1) {
803 error = EINVAL;
804 goto out;
805 }
806 mc.dev = di->source;
807 error = copyin(SCARG(uap, data), &idat, sizeof idat);
808 if (error)
809 goto out;
810 if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
811 mc.type = AUDIO_MIXER_ENUM;
812 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
813 if (idat & (1 << i))
814 break;
815 if (i >= OSS_SOUND_MIXER_NRDEVICES ||
816 di->devmap[i] == -1)
817 return EINVAL;
818 mc.un.ord = enum_to_ord(di, di->devmap[i]);
819 } else {
820 mc.type = AUDIO_MIXER_SET;
821 mc.un.mask = 0;
822 for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
823 if (idat & (1 << i)) {
824 if (di->devmap[i] == -1)
825 return EINVAL;
826 mc.un.mask |= enum_to_mask(di, di->devmap[i]);
827 }
828 }
829 }
830 error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
831 goto out;
832 default:
833 if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
834 com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
835 n = OSS_GET_DEV(com);
836 if (di->devmap[n] == -1) {
837 error = EINVAL;
838 goto out;
839 }
840 doread:
841 mc.dev = di->devmap[n];
842 mc.type = AUDIO_MIXER_VALUE;
843 mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
844 error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
845 if (error)
846 goto out;
847 if (mc.un.value.num_channels != 2) {
848 l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
849 } else {
850 l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
851 r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
852 }
853 idat = TO_OSSVOL(l) | (TO_OSSVOL(r) << 8);
854 DPRINTF(("OSS_MIXER_READ n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
855 n, di->devmap[n], l, r, idat));
856 break;
857 } else if ((OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_FIRST) <= com &&
858 com < OSS_MIXER_WRITE_R(OSS_SOUND_MIXER_NRDEVICES)) ||
859 (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
860 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES))) {
861 n = OSS_GET_DEV(com);
862 if (di->devmap[n] == -1) {
863 error = EINVAL;
864 goto out;
865 }
866 error = copyin(SCARG(uap, data), &idat, sizeof idat);
867 if (error)
868 goto out;
869 l = FROM_OSSVOL( idat & 0xff);
870 r = FROM_OSSVOL((idat >> 8) & 0xff);
871 mc.dev = di->devmap[n];
872 mc.type = AUDIO_MIXER_VALUE;
873 if (di->stereomask & (1<<n)) {
874 mc.un.value.num_channels = 2;
875 mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
876 mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
877 } else {
878 mc.un.value.num_channels = 1;
879 mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
880 }
881 DPRINTF(("OSS_MIXER_WRITE n=%d (dev=%d) l=%d, r=%d, idat=%04x\n",
882 n, di->devmap[n], l, r, idat));
883 error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
884 if (error)
885 goto out;
886 if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
887 com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
888 error = 0;
889 goto out;
890 }
891 goto doread;
892 } else {
893 #ifdef AUDIO_DEBUG
894 printf("oss_audio: unknown mixer ioctl %04lx\n", com);
895 #endif
896 error = EINVAL;
897 goto out;
898 }
899 }
900 error = copyout(&idat, SCARG(uap, data), sizeof idat);
901 out:
902 FILE_UNUSE(fp, p);
903 return error;
904 }
905
906 /* Sequencer emulation */
907 int
908 oss_ioctl_sequencer(p, uap, retval)
909 struct proc *p;
910 struct oss_sys_ioctl_args /* {
911 syscallarg(int) fd;
912 syscallarg(u_long) com;
913 syscallarg(caddr_t) data;
914 } */ *uap;
915 register_t *retval;
916 {
917 struct file *fp;
918 struct filedesc *fdp;
919 u_long com;
920 int idat, idat1;
921 struct synth_info si;
922 struct oss_synth_info osi;
923 struct oss_seq_event_rec oser;
924 int error;
925 int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
926
927 fdp = p->p_fd;
928 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
929 return (EBADF);
930
931 FILE_USE(fp);
932
933 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
934 error = EBADF;
935 goto out;
936 }
937
938 com = SCARG(uap, com);
939 DPRINTF(("oss_ioctl_sequencer: com=%08lx\n", com));
940
941 retval[0] = 0;
942
943 ioctlf = fp->f_ops->fo_ioctl;
944 switch (com) {
945 case OSS_SEQ_RESET:
946 error = ioctlf(fp, SEQUENCER_RESET, (caddr_t)&idat, p);
947 goto out;
948 case OSS_SEQ_SYNC:
949 error = ioctlf(fp, SEQUENCER_SYNC, (caddr_t)&idat, p);
950 goto out;
951 case OSS_SYNTH_INFO:
952 error = copyin(SCARG(uap, data), &osi, sizeof osi);
953 if (error)
954 goto out;
955 si.device = osi.device;
956 error = ioctlf(fp, SEQUENCER_INFO, (caddr_t)&si, p);
957 if (error)
958 goto out;
959 strncpy(osi.name, si.name, sizeof osi.name);
960 osi.device = si.device;
961 switch(si.synth_type) {
962 case SYNTH_TYPE_FM:
963 osi.synth_type = OSS_SYNTH_TYPE_FM; break;
964 case SYNTH_TYPE_SAMPLE:
965 osi.synth_type = OSS_SYNTH_TYPE_SAMPLE; break;
966 case SYNTH_TYPE_MIDI:
967 osi.synth_type = OSS_SYNTH_TYPE_MIDI; break;
968 default:
969 osi.synth_type = 0; break;
970 }
971 switch(si.synth_subtype) {
972 case SYNTH_SUB_FM_TYPE_ADLIB:
973 osi.synth_subtype = OSS_FM_TYPE_ADLIB; break;
974 case SYNTH_SUB_FM_TYPE_OPL3:
975 osi.synth_subtype = OSS_FM_TYPE_OPL3; break;
976 case SYNTH_SUB_MIDI_TYPE_MPU401:
977 osi.synth_subtype = OSS_MIDI_TYPE_MPU401; break;
978 case SYNTH_SUB_SAMPLE_TYPE_BASIC:
979 osi.synth_subtype = OSS_SAMPLE_TYPE_BASIC; break;
980 default:
981 osi.synth_subtype = 0; break;
982 }
983 osi.perc_mode = 0;
984 osi.nr_voices = si.nr_voices;
985 osi.nr_drums = 0;
986 osi.instr_bank_size = si.instr_bank_size;
987 osi.capabilities = 0;
988 if (si.capabilities & SYNTH_CAP_OPL3)
989 osi.capabilities |= OSS_SYNTH_CAP_OPL3;
990 if (si.capabilities & SYNTH_CAP_INPUT)
991 osi.capabilities |= OSS_SYNTH_CAP_INPUT;
992 error = copyout(&osi, SCARG(uap, data), sizeof osi);
993 goto out;
994 case OSS_SEQ_CTRLRATE:
995 error = copyin(SCARG(uap, data), &idat, sizeof idat);
996 if (error)
997 goto out;
998 error = ioctlf(fp, SEQUENCER_CTRLRATE, (caddr_t)&idat, p);
999 if (error)
1000 goto out;
1001 retval[0] = idat;
1002 break;
1003 case OSS_SEQ_GETOUTCOUNT:
1004 error = ioctlf(fp, SEQUENCER_GETOUTCOUNT, (caddr_t)&idat, p);
1005 if (error)
1006 goto out;
1007 retval[0] = idat;
1008 break;
1009 case OSS_SEQ_GETINCOUNT:
1010 error = ioctlf(fp, SEQUENCER_GETINCOUNT, (caddr_t)&idat, p);
1011 if (error)
1012 goto out;
1013 retval[0] = idat;
1014 break;
1015 case OSS_SEQ_NRSYNTHS:
1016 error = ioctlf(fp, SEQUENCER_NRSYNTHS, (caddr_t)&idat, p);
1017 if (error)
1018 goto out;
1019 retval[0] = idat;
1020 break;
1021 case OSS_SEQ_NRMIDIS:
1022 error = ioctlf(fp, SEQUENCER_NRMIDIS, (caddr_t)&idat, p);
1023 if (error)
1024 goto out;
1025 retval[0] = idat;
1026 break;
1027 case OSS_SEQ_THRESHOLD:
1028 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1029 if (error)
1030 goto out;
1031 error = ioctlf(fp, SEQUENCER_THRESHOLD, (caddr_t)&idat, p);
1032 goto out;
1033 case OSS_MEMAVL:
1034 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1035 if (error)
1036 goto out;
1037 error = ioctlf(fp, SEQUENCER_MEMAVL, (caddr_t)&idat, p);
1038 if (error)
1039 goto out;
1040 retval[0] = idat;
1041 break;
1042 case OSS_SEQ_PANIC:
1043 error = ioctlf(fp, SEQUENCER_PANIC, (caddr_t)&idat, p);
1044 goto out;
1045 case OSS_SEQ_OUTOFBAND:
1046 error = copyin(SCARG(uap, data), &oser, sizeof oser);
1047 if (error)
1048 goto out;
1049 error = ioctlf(fp, SEQUENCER_OUTOFBAND, (caddr_t)&oser, p);
1050 if (error)
1051 goto out;
1052 break;
1053 case OSS_SEQ_GETTIME:
1054 error = ioctlf(fp, SEQUENCER_GETTIME, (caddr_t)&idat, p);
1055 if (error)
1056 goto out;
1057 retval[0] = idat;
1058 break;
1059 case OSS_TMR_TIMEBASE:
1060 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1061 if (error)
1062 goto out;
1063 error = ioctlf(fp, SEQUENCER_TMR_TIMEBASE, (caddr_t)&idat, p);
1064 if (error)
1065 goto out;
1066 retval[0] = idat;
1067 break;
1068 case OSS_TMR_START:
1069 error = ioctlf(fp, SEQUENCER_TMR_START, (caddr_t)&idat, p);
1070 goto out;
1071 case OSS_TMR_STOP:
1072 error = ioctlf(fp, SEQUENCER_TMR_STOP, (caddr_t)&idat, p);
1073 goto out;
1074 case OSS_TMR_CONTINUE:
1075 error = ioctlf(fp, SEQUENCER_TMR_CONTINUE, (caddr_t)&idat, p);
1076 goto out;
1077 case OSS_TMR_TEMPO:
1078 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1079 if (error)
1080 goto out;
1081 error = ioctlf(fp, SEQUENCER_TMR_TEMPO, (caddr_t)&idat, p);
1082 if (error)
1083 goto out;
1084 retval[0] = idat;
1085 break;
1086 case OSS_TMR_SOURCE:
1087 error = copyin(SCARG(uap, data), &idat1, sizeof idat);
1088 if (error)
1089 goto out;
1090 idat = 0;
1091 if (idat1 & OSS_TMR_INTERNAL) idat |= SEQUENCER_TMR_INTERNAL;
1092 error = ioctlf(fp, SEQUENCER_TMR_SOURCE, (caddr_t)&idat, p);
1093 if (error)
1094 goto out;
1095 idat1 = idat;
1096 if (idat1 & SEQUENCER_TMR_INTERNAL) idat |= OSS_TMR_INTERNAL;
1097 retval[0] = idat;
1098 break;
1099 case OSS_TMR_METRONOME:
1100 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1101 if (error)
1102 goto out;
1103 error = ioctlf(fp, SEQUENCER_TMR_METRONOME, (caddr_t)&idat, p);
1104 goto out;
1105 case OSS_TMR_SELECT:
1106 error = copyin(SCARG(uap, data), &idat, sizeof idat);
1107 if (error)
1108 goto out;
1109 retval[0] = idat;
1110 error = ioctlf(fp, SEQUENCER_TMR_SELECT, (caddr_t)&idat, p);
1111 goto out;
1112 default:
1113 error = EINVAL;
1114 goto out;
1115 }
1116
1117 error = copyout(&idat, SCARG(uap, data), sizeof idat);
1118 out:
1119 FILE_UNUSE(fp, p);
1120 return error;
1121 }
1122
1123 /*
1124 * Check that the blocksize is a power of 2 as OSS wants.
1125 * If not, set it to be.
1126 */
1127 static void
1128 setblocksize(fp, info, p)
1129 struct file *fp;
1130 struct audio_info *info;
1131 struct proc *p;
1132 {
1133 struct audio_info set;
1134 int s;
1135
1136 if (info->blocksize & (info->blocksize-1)) {
1137 for(s = 32; s < info->blocksize; s <<= 1)
1138 ;
1139 AUDIO_INITINFO(&set);
1140 set.blocksize = s;
1141 fp->f_ops->fo_ioctl(fp, AUDIO_SETINFO, (caddr_t)&set, p);
1142 fp->f_ops->fo_ioctl(fp, AUDIO_GETINFO, (caddr_t)info, p);
1143 }
1144 }
1145