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