ossaudio.c revision 1.13 1 1.13 augustss /* $NetBSD: ossaudio.c,v 1.13 1997/07/27 01:16:41 augustss Exp $ */
2 1.1 mycroft #include <sys/param.h>
3 1.1 mycroft #include <sys/proc.h>
4 1.1 mycroft #include <sys/systm.h>
5 1.1 mycroft #include <sys/file.h>
6 1.6 augustss #include <sys/vnode.h>
7 1.1 mycroft #include <sys/filedesc.h>
8 1.1 mycroft #include <sys/ioctl.h>
9 1.1 mycroft #include <sys/mount.h>
10 1.1 mycroft #include <sys/audioio.h>
11 1.1 mycroft
12 1.1 mycroft #include <sys/syscallargs.h>
13 1.1 mycroft
14 1.6 augustss #include <compat/ossaudio/ossaudio.h>
15 1.6 augustss #include <compat/ossaudio/ossaudiovar.h>
16 1.6 augustss
17 1.6 augustss static struct audiodevinfo *getdevinfo __P((struct file *, struct proc *));
18 1.1 mycroft
19 1.1 mycroft int
20 1.6 augustss oss_ioctl_audio(p, uap, retval)
21 1.1 mycroft register struct proc *p;
22 1.6 augustss register struct oss_sys_ioctl_args /* {
23 1.1 mycroft syscallarg(int) fd;
24 1.1 mycroft syscallarg(u_long) com;
25 1.1 mycroft syscallarg(caddr_t) data;
26 1.1 mycroft } */ *uap;
27 1.1 mycroft register_t *retval;
28 1.1 mycroft {
29 1.1 mycroft register struct file *fp;
30 1.1 mycroft register struct filedesc *fdp;
31 1.1 mycroft u_long com;
32 1.1 mycroft struct audio_info tmpinfo;
33 1.13 augustss struct audio_offset tmpoffs;
34 1.13 augustss struct oss_audio_buf_info bufinfo;
35 1.13 augustss struct oss_count_info cntinfo;
36 1.13 augustss int idat, idata;
37 1.1 mycroft int error;
38 1.6 augustss int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
39 1.1 mycroft
40 1.1 mycroft fdp = p->p_fd;
41 1.1 mycroft if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
42 1.1 mycroft (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
43 1.1 mycroft return (EBADF);
44 1.1 mycroft
45 1.1 mycroft if ((fp->f_flag & (FREAD | FWRITE)) == 0)
46 1.1 mycroft return (EBADF);
47 1.1 mycroft
48 1.6 augustss ioctlf = fp->f_ops->fo_ioctl;
49 1.6 augustss
50 1.1 mycroft com = SCARG(uap, com);
51 1.1 mycroft retval[0] = 0;
52 1.1 mycroft
53 1.1 mycroft switch (com) {
54 1.6 augustss case OSS_SNDCTL_DSP_RESET:
55 1.6 augustss error = ioctlf(fp, AUDIO_FLUSH, (caddr_t)0, p);
56 1.1 mycroft if (error)
57 1.1 mycroft return error;
58 1.1 mycroft break;
59 1.6 augustss case OSS_SNDCTL_DSP_SYNC:
60 1.6 augustss case OSS_SNDCTL_DSP_POST:
61 1.6 augustss error = ioctlf(fp, AUDIO_DRAIN, (caddr_t)0, p);
62 1.1 mycroft if (error)
63 1.1 mycroft return error;
64 1.1 mycroft break;
65 1.6 augustss case OSS_SNDCTL_DSP_SPEED:
66 1.1 mycroft AUDIO_INITINFO(&tmpinfo);
67 1.1 mycroft error = copyin(SCARG(uap, data), &idat, sizeof idat);
68 1.1 mycroft if (error)
69 1.1 mycroft return error;
70 1.1 mycroft tmpinfo.play.sample_rate =
71 1.1 mycroft tmpinfo.record.sample_rate = idat;
72 1.6 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
73 1.6 augustss /* fall into ... */
74 1.6 augustss case OSS_SOUND_PCM_READ_RATE:
75 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
76 1.1 mycroft if (error)
77 1.1 mycroft return error;
78 1.1 mycroft idat = tmpinfo.play.sample_rate;
79 1.1 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
80 1.1 mycroft if (error)
81 1.1 mycroft return error;
82 1.1 mycroft break;
83 1.6 augustss case OSS_SNDCTL_DSP_STEREO:
84 1.1 mycroft AUDIO_INITINFO(&tmpinfo);
85 1.1 mycroft error = copyin(SCARG(uap, data), &idat, sizeof idat);
86 1.1 mycroft if (error)
87 1.1 mycroft return error;
88 1.1 mycroft tmpinfo.play.channels =
89 1.1 mycroft tmpinfo.record.channels = idat ? 2 : 1;
90 1.6 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
91 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
92 1.1 mycroft if (error)
93 1.1 mycroft return error;
94 1.1 mycroft idat = tmpinfo.play.channels - 1;
95 1.1 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
96 1.1 mycroft if (error)
97 1.1 mycroft return error;
98 1.1 mycroft break;
99 1.6 augustss case OSS_SNDCTL_DSP_GETBLKSIZE:
100 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
101 1.1 mycroft if (error)
102 1.1 mycroft return error;
103 1.1 mycroft idat = tmpinfo.blocksize;
104 1.1 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
105 1.1 mycroft if (error)
106 1.1 mycroft return error;
107 1.1 mycroft break;
108 1.6 augustss case OSS_SNDCTL_DSP_SETFMT:
109 1.1 mycroft AUDIO_INITINFO(&tmpinfo);
110 1.1 mycroft error = copyin(SCARG(uap, data), &idat, sizeof idat);
111 1.1 mycroft if (error)
112 1.1 mycroft return error;
113 1.1 mycroft switch (idat) {
114 1.6 augustss case OSS_AFMT_MU_LAW:
115 1.1 mycroft tmpinfo.play.precision =
116 1.1 mycroft tmpinfo.record.precision = 8;
117 1.1 mycroft tmpinfo.play.encoding =
118 1.1 mycroft tmpinfo.record.encoding = AUDIO_ENCODING_ULAW;
119 1.1 mycroft break;
120 1.6 augustss case OSS_AFMT_A_LAW:
121 1.1 mycroft tmpinfo.play.precision =
122 1.1 mycroft tmpinfo.record.precision = 8;
123 1.1 mycroft tmpinfo.play.encoding =
124 1.1 mycroft tmpinfo.record.encoding = AUDIO_ENCODING_ALAW;
125 1.1 mycroft break;
126 1.6 augustss case OSS_AFMT_U8:
127 1.1 mycroft tmpinfo.play.precision =
128 1.1 mycroft tmpinfo.record.precision = 8;
129 1.1 mycroft tmpinfo.play.encoding =
130 1.8 augustss tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR;
131 1.8 augustss break;
132 1.8 augustss case OSS_AFMT_S8:
133 1.8 augustss tmpinfo.play.precision =
134 1.8 augustss tmpinfo.record.precision = 8;
135 1.8 augustss tmpinfo.play.encoding =
136 1.12 augustss tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR;
137 1.1 mycroft break;
138 1.6 augustss case OSS_AFMT_S16_LE:
139 1.1 mycroft tmpinfo.play.precision =
140 1.1 mycroft tmpinfo.record.precision = 16;
141 1.1 mycroft tmpinfo.play.encoding =
142 1.12 augustss tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_LE;
143 1.8 augustss break;
144 1.8 augustss case OSS_AFMT_S16_BE:
145 1.8 augustss tmpinfo.play.precision =
146 1.8 augustss tmpinfo.record.precision = 16;
147 1.8 augustss tmpinfo.play.encoding =
148 1.12 augustss tmpinfo.record.encoding = AUDIO_ENCODING_SLINEAR_BE;
149 1.8 augustss break;
150 1.8 augustss case OSS_AFMT_U16_LE:
151 1.8 augustss tmpinfo.play.precision =
152 1.8 augustss tmpinfo.record.precision = 16;
153 1.8 augustss tmpinfo.play.encoding =
154 1.8 augustss tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_LE;
155 1.8 augustss break;
156 1.8 augustss case OSS_AFMT_U16_BE:
157 1.8 augustss tmpinfo.play.precision =
158 1.8 augustss tmpinfo.record.precision = 16;
159 1.8 augustss tmpinfo.play.encoding =
160 1.8 augustss tmpinfo.record.encoding = AUDIO_ENCODING_ULINEAR_BE;
161 1.1 mycroft break;
162 1.1 mycroft default:
163 1.1 mycroft return EINVAL;
164 1.1 mycroft }
165 1.6 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
166 1.6 augustss /* fall into ... */
167 1.6 augustss case OSS_SOUND_PCM_READ_BITS:
168 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
169 1.1 mycroft if (error)
170 1.1 mycroft return error;
171 1.5 mycroft switch (tmpinfo.play.encoding) {
172 1.5 mycroft case AUDIO_ENCODING_ULAW:
173 1.6 augustss idat = OSS_AFMT_MU_LAW;
174 1.5 mycroft break;
175 1.5 mycroft case AUDIO_ENCODING_ALAW:
176 1.6 augustss idat = OSS_AFMT_A_LAW;
177 1.5 mycroft break;
178 1.12 augustss case AUDIO_ENCODING_SLINEAR_LE:
179 1.8 augustss if (tmpinfo.play.precision == 16)
180 1.8 augustss idat = OSS_AFMT_S16_LE;
181 1.8 augustss else
182 1.8 augustss idat = OSS_AFMT_S8;
183 1.8 augustss break;
184 1.12 augustss case AUDIO_ENCODING_SLINEAR_BE:
185 1.8 augustss if (tmpinfo.play.precision == 16)
186 1.8 augustss idat = OSS_AFMT_S16_BE;
187 1.8 augustss else
188 1.8 augustss idat = OSS_AFMT_S8;
189 1.8 augustss break;
190 1.8 augustss case AUDIO_ENCODING_ULINEAR_LE:
191 1.8 augustss if (tmpinfo.play.precision == 16)
192 1.8 augustss idat = OSS_AFMT_U16_LE;
193 1.8 augustss else
194 1.8 augustss idat = OSS_AFMT_U8;
195 1.8 augustss break;
196 1.8 augustss case AUDIO_ENCODING_ULINEAR_BE:
197 1.8 augustss if (tmpinfo.play.precision == 16)
198 1.8 augustss idat = OSS_AFMT_U16_BE;
199 1.8 augustss else
200 1.8 augustss idat = OSS_AFMT_U8;
201 1.5 mycroft break;
202 1.5 mycroft case AUDIO_ENCODING_ADPCM:
203 1.6 augustss idat = OSS_AFMT_IMA_ADPCM;
204 1.5 mycroft break;
205 1.5 mycroft }
206 1.5 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
207 1.5 mycroft if (error)
208 1.5 mycroft return error;
209 1.1 mycroft break;
210 1.6 augustss case OSS_SNDCTL_DSP_CHANNELS:
211 1.6 augustss AUDIO_INITINFO(&tmpinfo);
212 1.6 augustss error = copyin(SCARG(uap, data), &idat, sizeof idat);
213 1.6 augustss if (error)
214 1.6 augustss return error;
215 1.6 augustss tmpinfo.play.channels =
216 1.6 augustss tmpinfo.record.channels = idat;
217 1.6 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
218 1.6 augustss /* fall into ... */
219 1.6 augustss case OSS_SOUND_PCM_READ_CHANNELS:
220 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
221 1.6 augustss if (error)
222 1.6 augustss return error;
223 1.6 augustss idat = tmpinfo.play.channels;
224 1.6 augustss error = copyout(&idat, SCARG(uap, data), sizeof idat);
225 1.6 augustss if (error)
226 1.6 augustss return error;
227 1.6 augustss break;
228 1.6 augustss case OSS_SOUND_PCM_WRITE_FILTER:
229 1.6 augustss case OSS_SOUND_PCM_READ_FILTER:
230 1.6 augustss return EINVAL; /* XXX unimplemented */
231 1.6 augustss case OSS_SNDCTL_DSP_SUBDIVIDE:
232 1.9 augustss error = copyin(SCARG(uap, data), &idat, sizeof idat);
233 1.9 augustss if (error)
234 1.9 augustss return error;
235 1.9 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
236 1.9 augustss if (error)
237 1.9 augustss return error;
238 1.9 augustss if (idat == 0)
239 1.9 augustss idat = tmpinfo.buffersize / tmpinfo.blocksize;
240 1.9 augustss idat = (tmpinfo.buffersize / idat) & -4;
241 1.9 augustss AUDIO_INITINFO(&tmpinfo);
242 1.9 augustss tmpinfo.blocksize = idat;
243 1.9 augustss error = ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
244 1.9 augustss if (error)
245 1.9 augustss return error;
246 1.9 augustss idat = tmpinfo.buffersize / tmpinfo.blocksize;
247 1.9 augustss error = copyout(&idat, SCARG(uap, data), sizeof idat);
248 1.9 augustss if (error)
249 1.9 augustss return error;
250 1.9 augustss break;
251 1.6 augustss case OSS_SNDCTL_DSP_SETFRAGMENT:
252 1.1 mycroft AUDIO_INITINFO(&tmpinfo);
253 1.1 mycroft error = copyin(SCARG(uap, data), &idat, sizeof idat);
254 1.1 mycroft if (error)
255 1.1 mycroft return error;
256 1.1 mycroft if ((idat & 0xffff) < 4 || (idat & 0xffff) > 17)
257 1.1 mycroft return EINVAL;
258 1.1 mycroft tmpinfo.blocksize = 1 << (idat & 0xffff);
259 1.1 mycroft tmpinfo.hiwat = (idat >> 16) & 0xffff;
260 1.6 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
261 1.6 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
262 1.1 mycroft if (error)
263 1.1 mycroft return error;
264 1.1 mycroft idat = tmpinfo.blocksize;
265 1.1 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
266 1.1 mycroft if (error)
267 1.1 mycroft return error;
268 1.1 mycroft break;
269 1.6 augustss case OSS_SNDCTL_DSP_GETFMTS:
270 1.6 augustss idat = OSS_AFMT_MU_LAW | OSS_AFMT_U8 | OSS_AFMT_S16_LE;
271 1.6 augustss error = copyout(&idat, SCARG(uap, data), sizeof idat);
272 1.6 augustss if (error)
273 1.6 augustss return error;
274 1.6 augustss break;
275 1.6 augustss case OSS_SNDCTL_DSP_GETOSPACE:
276 1.13 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
277 1.13 augustss if (error)
278 1.13 augustss return error;
279 1.13 augustss bufinfo.fragsize = tmpinfo.blocksize;
280 1.13 augustss bufinfo.fragments = /* XXX */
281 1.13 augustss bufinfo.fragstotal = tmpinfo.buffersize / bufinfo.fragsize;
282 1.13 augustss bufinfo.bytes = bufinfo.fragments * bufinfo.fragsize;
283 1.13 augustss error = copyout(&bufinfo, SCARG(uap, data), sizeof bufinfo);
284 1.13 augustss if (error)
285 1.13 augustss return error;
286 1.13 augustss break;
287 1.6 augustss case OSS_SNDCTL_DSP_GETISPACE:
288 1.6 augustss case OSS_SNDCTL_DSP_NONBLOCK:
289 1.6 augustss return EINVAL; /* XXX unimplemented */
290 1.6 augustss case OSS_SNDCTL_DSP_GETCAPS:
291 1.13 augustss error = ioctlf(fp, AUDIO_GETPROPS, (caddr_t)&idata, p);
292 1.13 augustss if (error)
293 1.13 augustss return error;
294 1.13 augustss idat = OSS_DSP_CAP_TRIGGER; /* pretend we have trigger */
295 1.13 augustss if (idata & AUDIO_PROP_FULLDUPLEX)
296 1.13 augustss idat |= OSS_DSP_CAP_DUPLEX;
297 1.13 augustss if (idata & AUDIO_PROP_MMAP)
298 1.13 augustss idat |= OSS_DSP_CAP_MMAP;
299 1.13 augustss error = copyout(&idat, SCARG(uap, data), sizeof idat);
300 1.13 augustss if (error)
301 1.13 augustss return error;
302 1.13 augustss break;
303 1.13 augustss #if 0
304 1.13 augustss case OSS_SNDCTL_DSP_GETTRIGGER:
305 1.13 augustss error = ioctlf(fp, AUDIO_GETINFO, (caddr_t)&tmpinfo, p);
306 1.13 augustss if (error)
307 1.13 augustss return error;
308 1.13 augustss idat = (tmpinfo.play.pause ? 0 : OSS_PCM_ENABLE_OUTPUT) |
309 1.13 augustss (tmpinfo.record.pause ? 0 : OSS_PCM_ENABLE_INPUT);
310 1.13 augustss error = copyout(&idat, SCARG(uap, data), sizeof idat);
311 1.13 augustss if (error)
312 1.13 augustss return error;
313 1.13 augustss break;
314 1.13 augustss case OSS_SNDCTL_DSP_SETTRIGGER:
315 1.13 augustss AUDIO_INITINFO(&tmpinfo);
316 1.13 augustss error = copyin(SCARG(uap, data), &idat, sizeof idat);
317 1.13 augustss if (error)
318 1.13 augustss return error;
319 1.13 augustss tmpinfo.play.pause = (idat & OSS_PCM_ENABLE_OUTPUT) == 0;
320 1.13 augustss tmpinfo.record.pause = (idat & OSS_PCM_ENABLE_INPUT) == 0;
321 1.13 augustss (void) ioctlf(fp, AUDIO_SETINFO, (caddr_t)&tmpinfo, p);
322 1.1 mycroft error = copyout(&idat, SCARG(uap, data), sizeof idat);
323 1.1 mycroft if (error)
324 1.1 mycroft return error;
325 1.1 mycroft break;
326 1.13 augustss #else
327 1.6 augustss case OSS_SNDCTL_DSP_GETTRIGGER:
328 1.6 augustss case OSS_SNDCTL_DSP_SETTRIGGER:
329 1.13 augustss /* XXX Do nothing for now. */
330 1.13 augustss idat = OSS_PCM_ENABLE_OUTPUT;
331 1.13 augustss return copyout(&idat, SCARG(uap, data), sizeof idat);
332 1.13 augustss #endif
333 1.6 augustss case OSS_SNDCTL_DSP_GETIPTR:
334 1.13 augustss error = ioctlf(fp, AUDIO_GETIOFFS, (caddr_t)&tmpoffs, p);
335 1.13 augustss if (error)
336 1.13 augustss return error;
337 1.13 augustss cntinfo.bytes = tmpoffs.samples;
338 1.13 augustss cntinfo.blocks = tmpoffs.deltablks;
339 1.13 augustss cntinfo.ptr = tmpoffs.offset;
340 1.13 augustss error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
341 1.13 augustss if (error)
342 1.13 augustss return error;
343 1.13 augustss break;
344 1.6 augustss case OSS_SNDCTL_DSP_GETOPTR:
345 1.13 augustss error = ioctlf(fp, AUDIO_GETOOFFS, (caddr_t)&tmpoffs, p);
346 1.13 augustss if (error)
347 1.13 augustss return error;
348 1.13 augustss cntinfo.bytes = tmpoffs.samples;
349 1.13 augustss cntinfo.blocks = tmpoffs.deltablks;
350 1.13 augustss cntinfo.ptr = tmpoffs.offset;
351 1.13 augustss error = copyout(&cntinfo, SCARG(uap, data), sizeof cntinfo);
352 1.13 augustss if (error)
353 1.13 augustss return error;
354 1.13 augustss break;
355 1.6 augustss case OSS_SNDCTL_DSP_MAPINBUF:
356 1.6 augustss case OSS_SNDCTL_DSP_MAPOUTBUF:
357 1.6 augustss case OSS_SNDCTL_DSP_SETSYNCRO:
358 1.6 augustss case OSS_SNDCTL_DSP_SETDUPLEX:
359 1.6 augustss case OSS_SNDCTL_DSP_PROFILE:
360 1.6 augustss return EINVAL; /* XXX unimplemented */
361 1.1 mycroft default:
362 1.1 mycroft return EINVAL;
363 1.1 mycroft }
364 1.1 mycroft
365 1.1 mycroft return 0;
366 1.3 mycroft }
367 1.3 mycroft
368 1.6 augustss /* If the NetBSD mixer device should have more than 32 devices
369 1.6 augustss * some will not be available to Linux */
370 1.6 augustss #define NETBSD_MAXDEVS 32
371 1.6 augustss struct audiodevinfo {
372 1.6 augustss int done;
373 1.6 augustss dev_t dev;
374 1.7 augustss int16_t devmap[OSS_SOUND_MIXER_NRDEVICES],
375 1.7 augustss rdevmap[NETBSD_MAXDEVS];
376 1.6 augustss u_long devmask, recmask, stereomask;
377 1.6 augustss u_long caps, source;
378 1.6 augustss };
379 1.6 augustss
380 1.6 augustss /*
381 1.6 augustss * Collect the audio device information to allow faster
382 1.6 augustss * emulation of the Linux mixer ioctls. Cache the information
383 1.6 augustss * to eliminate the overhead of repeating all the ioctls needed
384 1.6 augustss * to collect the information.
385 1.6 augustss */
386 1.6 augustss static struct audiodevinfo *
387 1.6 augustss getdevinfo(fp, p)
388 1.6 augustss struct file *fp;
389 1.6 augustss struct proc *p;
390 1.6 augustss {
391 1.6 augustss mixer_devinfo_t mi;
392 1.6 augustss int i;
393 1.6 augustss static struct {
394 1.6 augustss char *name;
395 1.6 augustss int code;
396 1.6 augustss } *dp, devs[] = {
397 1.6 augustss { AudioNmicrophone, OSS_SOUND_MIXER_MIC },
398 1.6 augustss { AudioNline, OSS_SOUND_MIXER_LINE },
399 1.6 augustss { AudioNcd, OSS_SOUND_MIXER_CD },
400 1.6 augustss { AudioNdac, OSS_SOUND_MIXER_PCM },
401 1.6 augustss { AudioNrecord, OSS_SOUND_MIXER_IMIX },
402 1.6 augustss { AudioNvolume, OSS_SOUND_MIXER_VOLUME },
403 1.6 augustss { AudioNtreble, OSS_SOUND_MIXER_TREBLE },
404 1.6 augustss { AudioNbass, OSS_SOUND_MIXER_BASS },
405 1.6 augustss { AudioNspeaker, OSS_SOUND_MIXER_SPEAKER },
406 1.6 augustss /* { AudioNheadphone, ?? },*/
407 1.6 augustss { AudioNoutput, OSS_SOUND_MIXER_OGAIN },
408 1.6 augustss { AudioNinput, OSS_SOUND_MIXER_IGAIN },
409 1.10 augustss { AudioNmaster, OSS_SOUND_MIXER_SPEAKER },
410 1.6 augustss /* { AudioNstereo, ?? },*/
411 1.6 augustss /* { AudioNmono, ?? },*/
412 1.6 augustss { AudioNfmsynth, OSS_SOUND_MIXER_SYNTH },
413 1.6 augustss /* { AudioNwave, OSS_SOUND_MIXER_PCM },*/
414 1.10 augustss { AudioNmidi, OSS_SOUND_MIXER_SYNTH },
415 1.6 augustss /* { AudioNmixerout, ?? },*/
416 1.6 augustss { 0, -1 }
417 1.6 augustss };
418 1.6 augustss int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *)) =
419 1.6 augustss fp->f_ops->fo_ioctl;
420 1.6 augustss struct vnode *vp;
421 1.6 augustss struct vattr va;
422 1.6 augustss static struct audiodevinfo devcache = { 0 };
423 1.6 augustss struct audiodevinfo *di = &devcache;
424 1.6 augustss
425 1.6 augustss /* Figure out what device it is so we can check if the
426 1.6 augustss * cached data is valid.
427 1.6 augustss */
428 1.6 augustss vp = (struct vnode *)fp->f_data;
429 1.6 augustss if (vp->v_type != VCHR)
430 1.6 augustss return 0;
431 1.6 augustss if (VOP_GETATTR(vp, &va, p->p_ucred, p))
432 1.6 augustss return 0;
433 1.6 augustss if (di->done && di->dev == va.va_rdev)
434 1.6 augustss return di;
435 1.6 augustss
436 1.6 augustss di->done = 1;
437 1.6 augustss di->dev = va.va_rdev;
438 1.6 augustss di->devmask = 0;
439 1.6 augustss di->recmask = 0;
440 1.6 augustss di->stereomask = 0;
441 1.6 augustss di->source = -1;
442 1.6 augustss di->caps = 0;
443 1.6 augustss for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
444 1.6 augustss di->devmap[i] = -1;
445 1.6 augustss for(i = 0; i < NETBSD_MAXDEVS; i++)
446 1.6 augustss di->rdevmap[i] = -1;
447 1.6 augustss for(i = 0; i < NETBSD_MAXDEVS; i++) {
448 1.6 augustss mi.index = i;
449 1.6 augustss if (ioctlf(fp, AUDIO_MIXER_DEVINFO, (caddr_t)&mi, p) < 0)
450 1.6 augustss break;
451 1.6 augustss switch(mi.type) {
452 1.6 augustss case AUDIO_MIXER_VALUE:
453 1.6 augustss for(dp = devs; dp->name; dp++)
454 1.6 augustss if (strcmp(dp->name, mi.label.name) == 0)
455 1.6 augustss break;
456 1.6 augustss if (dp->code >= 0) {
457 1.6 augustss di->devmap[dp->code] = i;
458 1.6 augustss di->rdevmap[i] = dp->code;
459 1.6 augustss di->devmask |= 1 << dp->code;
460 1.6 augustss if (mi.un.v.num_channels == 2)
461 1.6 augustss di->stereomask |= 1 << dp->code;
462 1.6 augustss }
463 1.6 augustss break;
464 1.6 augustss case AUDIO_MIXER_ENUM:
465 1.6 augustss if (strcmp(mi.label.name, AudioNsource) == 0) {
466 1.6 augustss int j;
467 1.6 augustss di->source = i;
468 1.10 augustss for(j = 0; j < mi.un.e.num_mem; j++)
469 1.6 augustss di->recmask |= 1 << di->rdevmap[mi.un.e.member[j].ord];
470 1.6 augustss di->caps = OSS_SOUND_CAP_EXCL_INPUT;
471 1.6 augustss }
472 1.6 augustss break;
473 1.6 augustss case AUDIO_MIXER_SET:
474 1.6 augustss if (strcmp(mi.label.name, AudioNsource) == 0) {
475 1.6 augustss int j;
476 1.6 augustss di->source = i;
477 1.6 augustss for(j = 0; j < mi.un.s.num_mem; j++) {
478 1.6 augustss int k, mask = mi.un.s.member[j].mask;
479 1.6 augustss if (mask) {
480 1.10 augustss for(k = 0; !(mask & 1); mask >>= 1, k++)
481 1.6 augustss ;
482 1.6 augustss di->recmask |= 1 << di->rdevmap[k];
483 1.6 augustss }
484 1.6 augustss }
485 1.6 augustss }
486 1.6 augustss break;
487 1.6 augustss }
488 1.6 augustss }
489 1.6 augustss return di;
490 1.6 augustss }
491 1.6 augustss
492 1.6 augustss int
493 1.6 augustss oss_ioctl_mixer(p, uap, retval)
494 1.6 augustss register struct proc *p;
495 1.6 augustss register struct oss_sys_ioctl_args /* {
496 1.6 augustss syscallarg(int) fd;
497 1.6 augustss syscallarg(u_long) com;
498 1.6 augustss syscallarg(caddr_t) data;
499 1.6 augustss } */ *uap;
500 1.6 augustss register_t *retval;
501 1.6 augustss {
502 1.6 augustss struct file *fp;
503 1.6 augustss struct filedesc *fdp;
504 1.6 augustss u_long com;
505 1.6 augustss struct audiodevinfo *di;
506 1.6 augustss mixer_ctrl_t mc;
507 1.6 augustss int idat;
508 1.6 augustss int i;
509 1.6 augustss int error;
510 1.6 augustss int l, r, n;
511 1.6 augustss int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
512 1.6 augustss
513 1.6 augustss fdp = p->p_fd;
514 1.6 augustss if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
515 1.6 augustss (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
516 1.6 augustss return (EBADF);
517 1.6 augustss
518 1.6 augustss if ((fp->f_flag & (FREAD | FWRITE)) == 0)
519 1.6 augustss return (EBADF);
520 1.6 augustss
521 1.6 augustss com = SCARG(uap, com);
522 1.6 augustss retval[0] = 0;
523 1.6 augustss
524 1.6 augustss di = getdevinfo(fp, p);
525 1.6 augustss if (di == 0)
526 1.6 augustss return EINVAL;
527 1.6 augustss
528 1.6 augustss ioctlf = fp->f_ops->fo_ioctl;
529 1.6 augustss switch (com) {
530 1.6 augustss case OSS_SOUND_MIXER_READ_RECSRC:
531 1.6 augustss if (di->source == -1)
532 1.6 augustss return EINVAL;
533 1.6 augustss mc.dev = di->source;
534 1.6 augustss if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
535 1.6 augustss mc.type = AUDIO_MIXER_ENUM;
536 1.6 augustss error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
537 1.6 augustss if (error)
538 1.6 augustss return error;
539 1.6 augustss idat = 1 << di->rdevmap[mc.un.ord];
540 1.6 augustss } else {
541 1.6 augustss int k;
542 1.6 augustss unsigned int mask;
543 1.6 augustss mc.type = AUDIO_MIXER_SET;
544 1.6 augustss error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
545 1.6 augustss if (error)
546 1.6 augustss return error;
547 1.6 augustss idat = 0;
548 1.6 augustss for(mask = mc.un.mask, k = 0; mask; mask >>= 1, k++)
549 1.10 augustss if (mask & 1)
550 1.10 augustss idat |= 1 << di->rdevmap[k];
551 1.6 augustss }
552 1.6 augustss break;
553 1.6 augustss case OSS_SOUND_MIXER_READ_DEVMASK:
554 1.6 augustss idat = di->devmask;
555 1.6 augustss break;
556 1.6 augustss case OSS_SOUND_MIXER_READ_RECMASK:
557 1.6 augustss idat = di->recmask;
558 1.6 augustss break;
559 1.6 augustss case OSS_SOUND_MIXER_READ_STEREODEVS:
560 1.6 augustss idat = di->stereomask;
561 1.6 augustss break;
562 1.6 augustss case OSS_SOUND_MIXER_READ_CAPS:
563 1.6 augustss idat = di->caps;
564 1.6 augustss break;
565 1.6 augustss case OSS_SOUND_MIXER_WRITE_RECSRC:
566 1.6 augustss if (di->source == -1)
567 1.6 augustss return EINVAL;
568 1.6 augustss mc.dev = di->source;
569 1.6 augustss error = copyin(SCARG(uap, data), &idat, sizeof idat);
570 1.6 augustss if (error)
571 1.6 augustss return error;
572 1.7 augustss if (di->caps & OSS_SOUND_CAP_EXCL_INPUT) {
573 1.7 augustss mc.type = AUDIO_MIXER_ENUM;
574 1.7 augustss for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++)
575 1.7 augustss if (idat & (1 << i))
576 1.7 augustss break;
577 1.7 augustss if (i >= OSS_SOUND_MIXER_NRDEVICES ||
578 1.7 augustss di->devmap[i] == -1)
579 1.7 augustss return EINVAL;
580 1.7 augustss mc.un.ord = di->devmap[i];
581 1.7 augustss } else {
582 1.7 augustss mc.type = AUDIO_MIXER_SET;
583 1.11 augustss mc.un.mask = 0;
584 1.10 augustss for(i = 0; i < OSS_SOUND_MIXER_NRDEVICES; i++) {
585 1.7 augustss if (idat & (1 << i)) {
586 1.7 augustss if (di->devmap[i] == -1)
587 1.7 augustss return EINVAL;
588 1.7 augustss mc.un.mask |= 1 << di->devmap[i];
589 1.7 augustss }
590 1.10 augustss }
591 1.7 augustss }
592 1.7 augustss return ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
593 1.6 augustss default:
594 1.6 augustss if (OSS_MIXER_READ(OSS_SOUND_MIXER_FIRST) <= com &&
595 1.6 augustss com < OSS_MIXER_READ(OSS_SOUND_MIXER_NRDEVICES)) {
596 1.6 augustss n = OSS_GET_DEV(com);
597 1.7 augustss if (di->devmap[n] == -1)
598 1.7 augustss return EINVAL;
599 1.6 augustss mc.dev = di->devmap[n];
600 1.6 augustss mc.type = AUDIO_MIXER_VALUE;
601 1.6 augustss doread:
602 1.6 augustss mc.un.value.num_channels = di->stereomask & (1<<n) ? 2 : 1;
603 1.6 augustss error = ioctlf(fp, AUDIO_MIXER_READ, (caddr_t)&mc, p);
604 1.6 augustss if (error)
605 1.6 augustss return error;
606 1.6 augustss if (mc.type != AUDIO_MIXER_VALUE)
607 1.6 augustss return EINVAL;
608 1.6 augustss if (mc.un.value.num_channels != 2) {
609 1.6 augustss l = r = mc.un.value.level[AUDIO_MIXER_LEVEL_MONO];
610 1.6 augustss } else {
611 1.6 augustss l = mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT];
612 1.6 augustss r = mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
613 1.6 augustss }
614 1.6 augustss l = l * 100 / 255;
615 1.6 augustss r = r * 100 / 255;
616 1.6 augustss idat = l | (r << 8);
617 1.6 augustss break;
618 1.6 augustss } else if (OSS_MIXER_WRITE(OSS_SOUND_MIXER_FIRST) <= com &&
619 1.6 augustss com < OSS_MIXER_WRITE(OSS_SOUND_MIXER_NRDEVICES)) {
620 1.6 augustss n = OSS_GET_DEV(com);
621 1.7 augustss if (di->devmap[n] == -1)
622 1.7 augustss return EINVAL;
623 1.6 augustss error = copyin(SCARG(uap, data), &idat, sizeof idat);
624 1.6 augustss if (error)
625 1.6 augustss return error;
626 1.6 augustss l = (idat & 0xff) * 255 / 100;
627 1.6 augustss r = (idat >> 8) * 255 / 100;
628 1.6 augustss mc.dev = di->devmap[n];
629 1.6 augustss mc.type = AUDIO_MIXER_VALUE;
630 1.6 augustss if (di->stereomask & (1<<n)) {
631 1.6 augustss mc.un.value.num_channels = 2;
632 1.6 augustss mc.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
633 1.6 augustss mc.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
634 1.6 augustss } else {
635 1.6 augustss mc.un.value.num_channels = 1;
636 1.6 augustss mc.un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
637 1.6 augustss }
638 1.6 augustss error = ioctlf(fp, AUDIO_MIXER_WRITE, (caddr_t)&mc, p);
639 1.6 augustss if (error)
640 1.6 augustss return error;
641 1.6 augustss goto doread;
642 1.6 augustss } else
643 1.6 augustss return EINVAL;
644 1.6 augustss }
645 1.6 augustss return copyout(&idat, SCARG(uap, data), sizeof idat);
646 1.6 augustss }
647 1.6 augustss
648 1.6 augustss /* XXX hook for sequencer emulation */
649 1.3 mycroft int
650 1.6 augustss oss_ioctl_sequencer(p, uap, retval)
651 1.3 mycroft register struct proc *p;
652 1.6 augustss register struct oss_sys_ioctl_args /* {
653 1.3 mycroft syscallarg(int) fd;
654 1.3 mycroft syscallarg(u_long) com;
655 1.3 mycroft syscallarg(caddr_t) data;
656 1.3 mycroft } */ *uap;
657 1.3 mycroft register_t *retval;
658 1.3 mycroft {
659 1.3 mycroft register struct file *fp;
660 1.3 mycroft register struct filedesc *fdp;
661 1.6 augustss #if 0
662 1.3 mycroft u_long com;
663 1.3 mycroft int idat;
664 1.3 mycroft int error;
665 1.3 mycroft #endif
666 1.3 mycroft
667 1.3 mycroft fdp = p->p_fd;
668 1.3 mycroft if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
669 1.3 mycroft (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
670 1.3 mycroft return (EBADF);
671 1.3 mycroft
672 1.3 mycroft if ((fp->f_flag & (FREAD | FWRITE)) == 0)
673 1.3 mycroft return (EBADF);
674 1.3 mycroft
675 1.6 augustss #if 0
676 1.3 mycroft com = SCARG(uap, com);
677 1.6 augustss #endif
678 1.3 mycroft retval[0] = 0;
679 1.3 mycroft
680 1.6 augustss return EINVAL;
681 1.1 mycroft }
682