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