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