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