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