]>
Commit | Line | Data |
---|---|---|
85571bc7 | 1 | /* |
1d14ffa9 FB |
2 | * QEMU OSS audio driver |
3 | * | |
4 | * Copyright (c) 2003-2005 Vassili Karpov (malc) | |
5 | * | |
85571bc7 FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
f0c757e4 | 24 | #include <stdlib.h> |
85571bc7 FB |
25 | #include <sys/mman.h> |
26 | #include <sys/types.h> | |
27 | #include <sys/ioctl.h> | |
f0c757e4 TS |
28 | #ifdef __OpenBSD__ |
29 | #include <soundcard.h> | |
30 | #else | |
85571bc7 | 31 | #include <sys/soundcard.h> |
f0c757e4 | 32 | #endif |
87ecb68b | 33 | #include "qemu-common.h" |
057fa65c | 34 | #include "host-utils.h" |
dd8a5649 | 35 | #include "qemu-char.h" |
87ecb68b | 36 | #include "audio.h" |
fb065187 | 37 | |
1d14ffa9 FB |
38 | #define AUDIO_CAP "oss" |
39 | #include "audio_int.h" | |
fb065187 | 40 | |
1d14ffa9 FB |
41 | typedef struct OSSVoiceOut { |
42 | HWVoiceOut hw; | |
fb065187 FB |
43 | void *pcm_buf; |
44 | int fd; | |
9d168976 | 45 | int wpos; |
fb065187 FB |
46 | int nfrags; |
47 | int fragsize; | |
48 | int mmapped; | |
9d168976 | 49 | int pending; |
1d14ffa9 | 50 | } OSSVoiceOut; |
85571bc7 | 51 | |
1d14ffa9 FB |
52 | typedef struct OSSVoiceIn { |
53 | HWVoiceIn hw; | |
54 | void *pcm_buf; | |
55 | int fd; | |
56 | int nfrags; | |
57 | int fragsize; | |
1d14ffa9 | 58 | } OSSVoiceIn; |
85571bc7 FB |
59 | |
60 | static struct { | |
61 | int try_mmap; | |
62 | int nfrags; | |
63 | int fragsize; | |
1d14ffa9 FB |
64 | const char *devpath_out; |
65 | const char *devpath_in; | |
8ead62cf | 66 | int debug; |
0b3652bc | 67 | int exclusive; |
68 | int policy; | |
85571bc7 FB |
69 | } conf = { |
70 | .try_mmap = 0, | |
71 | .nfrags = 4, | |
72 | .fragsize = 4096, | |
1d14ffa9 | 73 | .devpath_out = "/dev/dsp", |
8ead62cf | 74 | .devpath_in = "/dev/dsp", |
0b3652bc | 75 | .debug = 0, |
76 | .exclusive = 0, | |
77 | .policy = 5 | |
85571bc7 FB |
78 | }; |
79 | ||
80 | struct oss_params { | |
81 | int freq; | |
82 | audfmt_e fmt; | |
83 | int nchannels; | |
84 | int nfrags; | |
85 | int fragsize; | |
86 | }; | |
87 | ||
1d14ffa9 | 88 | static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...) |
85571bc7 | 89 | { |
1d14ffa9 FB |
90 | va_list ap; |
91 | ||
571ec3d6 | 92 | va_start (ap, fmt); |
1d14ffa9 | 93 | AUD_vlog (AUDIO_CAP, fmt, ap); |
571ec3d6 | 94 | va_end (ap); |
1d14ffa9 | 95 | |
1d14ffa9 | 96 | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err)); |
85571bc7 FB |
97 | } |
98 | ||
1d14ffa9 FB |
99 | static void GCC_FMT_ATTR (3, 4) oss_logerr2 ( |
100 | int err, | |
101 | const char *typ, | |
102 | const char *fmt, | |
103 | ... | |
104 | ) | |
105 | { | |
106 | va_list ap; | |
107 | ||
c0fe3827 | 108 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
109 | |
110 | va_start (ap, fmt); | |
111 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
112 | va_end (ap); | |
113 | ||
114 | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err)); | |
115 | } | |
116 | ||
117 | static void oss_anal_close (int *fdp) | |
118 | { | |
6ebfda13 | 119 | int err; |
120 | ||
121 | qemu_set_fd_handler (*fdp, NULL, NULL, NULL); | |
122 | err = close (*fdp); | |
1d14ffa9 FB |
123 | if (err) { |
124 | oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp); | |
125 | } | |
126 | *fdp = -1; | |
127 | } | |
128 | ||
dd8a5649 | 129 | static void oss_helper_poll_out (void *opaque) |
130 | { | |
131 | (void) opaque; | |
132 | audio_run ("oss_poll_out"); | |
133 | } | |
134 | ||
135 | static void oss_helper_poll_in (void *opaque) | |
136 | { | |
137 | (void) opaque; | |
138 | audio_run ("oss_poll_in"); | |
139 | } | |
140 | ||
141 | static int oss_poll_out (HWVoiceOut *hw) | |
142 | { | |
143 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
144 | ||
145 | return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL); | |
146 | } | |
147 | ||
148 | static int oss_poll_in (HWVoiceIn *hw) | |
149 | { | |
150 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
151 | ||
152 | return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL); | |
153 | } | |
154 | ||
1d14ffa9 FB |
155 | static int oss_write (SWVoiceOut *sw, void *buf, int len) |
156 | { | |
157 | return audio_pcm_sw_write (sw, buf, len); | |
158 | } | |
159 | ||
160 | static int aud_to_ossfmt (audfmt_e fmt) | |
85571bc7 FB |
161 | { |
162 | switch (fmt) { | |
1d14ffa9 FB |
163 | case AUD_FMT_S8: |
164 | return AFMT_S8; | |
165 | ||
166 | case AUD_FMT_U8: | |
167 | return AFMT_U8; | |
168 | ||
169 | case AUD_FMT_S16: | |
170 | return AFMT_S16_LE; | |
171 | ||
172 | case AUD_FMT_U16: | |
173 | return AFMT_U16_LE; | |
174 | ||
85571bc7 | 175 | default: |
1d14ffa9 FB |
176 | dolog ("Internal logic error: Bad audio format %d\n", fmt); |
177 | #ifdef DEBUG_AUDIO | |
178 | abort (); | |
179 | #endif | |
180 | return AFMT_U8; | |
85571bc7 FB |
181 | } |
182 | } | |
183 | ||
1d14ffa9 | 184 | static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness) |
85571bc7 | 185 | { |
1d14ffa9 FB |
186 | switch (ossfmt) { |
187 | case AFMT_S8: | |
ca9cc28c | 188 | *endianness = 0; |
1d14ffa9 FB |
189 | *fmt = AUD_FMT_S8; |
190 | break; | |
191 | ||
192 | case AFMT_U8: | |
193 | *endianness = 0; | |
194 | *fmt = AUD_FMT_U8; | |
195 | break; | |
196 | ||
197 | case AFMT_S16_LE: | |
198 | *endianness = 0; | |
199 | *fmt = AUD_FMT_S16; | |
200 | break; | |
201 | ||
202 | case AFMT_U16_LE: | |
203 | *endianness = 0; | |
204 | *fmt = AUD_FMT_U16; | |
205 | break; | |
206 | ||
207 | case AFMT_S16_BE: | |
208 | *endianness = 1; | |
209 | *fmt = AUD_FMT_S16; | |
210 | break; | |
211 | ||
212 | case AFMT_U16_BE: | |
213 | *endianness = 1; | |
214 | *fmt = AUD_FMT_U16; | |
215 | break; | |
216 | ||
85571bc7 | 217 | default: |
1d14ffa9 FB |
218 | dolog ("Unrecognized audio format %d\n", ossfmt); |
219 | return -1; | |
85571bc7 | 220 | } |
1d14ffa9 FB |
221 | |
222 | return 0; | |
85571bc7 FB |
223 | } |
224 | ||
c0fe3827 | 225 | #if defined DEBUG_MISMATCHES || defined DEBUG |
1d14ffa9 | 226 | static void oss_dump_info (struct oss_params *req, struct oss_params *obt) |
85571bc7 FB |
227 | { |
228 | dolog ("parameter | requested value | obtained value\n"); | |
229 | dolog ("format | %10d | %10d\n", req->fmt, obt->fmt); | |
1d14ffa9 FB |
230 | dolog ("channels | %10d | %10d\n", |
231 | req->nchannels, obt->nchannels); | |
85571bc7 FB |
232 | dolog ("frequency | %10d | %10d\n", req->freq, obt->freq); |
233 | dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags); | |
1d14ffa9 FB |
234 | dolog ("fragsize | %10d | %10d\n", |
235 | req->fragsize, obt->fragsize); | |
85571bc7 FB |
236 | } |
237 | #endif | |
238 | ||
1d14ffa9 FB |
239 | static int oss_open (int in, struct oss_params *req, |
240 | struct oss_params *obt, int *pfd) | |
85571bc7 FB |
241 | { |
242 | int fd; | |
0b3652bc | 243 | int version; |
244 | int oflags = conf.exclusive ? O_EXCL : 0; | |
85571bc7 FB |
245 | audio_buf_info abinfo; |
246 | int fmt, freq, nchannels; | |
1d14ffa9 FB |
247 | const char *dspname = in ? conf.devpath_in : conf.devpath_out; |
248 | const char *typ = in ? "ADC" : "DAC"; | |
85571bc7 | 249 | |
2182349d | 250 | /* Kludge needed to have working mmap on Linux */ |
0b3652bc | 251 | oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY); |
252 | ||
2182349d | 253 | fd = open (dspname, oflags | O_NONBLOCK); |
85571bc7 | 254 | if (-1 == fd) { |
1d14ffa9 | 255 | oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname); |
85571bc7 FB |
256 | return -1; |
257 | } | |
258 | ||
259 | freq = req->freq; | |
260 | nchannels = req->nchannels; | |
261 | fmt = req->fmt; | |
262 | ||
263 | if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) { | |
1d14ffa9 | 264 | oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt); |
85571bc7 FB |
265 | goto err; |
266 | } | |
267 | ||
268 | if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) { | |
1d14ffa9 FB |
269 | oss_logerr2 (errno, typ, "Failed to set number of channels %d\n", |
270 | req->nchannels); | |
85571bc7 FB |
271 | goto err; |
272 | } | |
273 | ||
274 | if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) { | |
1d14ffa9 | 275 | oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq); |
85571bc7 FB |
276 | goto err; |
277 | } | |
278 | ||
902e2b51 | 279 | if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) { |
1d14ffa9 | 280 | oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n"); |
85571bc7 FB |
281 | goto err; |
282 | } | |
283 | ||
0b3652bc | 284 | if (ioctl (fd, OSS_GETVERSION, &version)) { |
285 | oss_logerr2 (errno, typ, "Failed to get OSS version\n"); | |
286 | version = 0; | |
287 | } | |
288 | ||
289 | if (conf.debug) { | |
290 | dolog ("OSS version = %#x\n", version); | |
291 | } | |
292 | ||
293 | #ifdef SNDCTL_DSP_POLICY | |
294 | if (conf.policy >= 0 && version >= 0x040000) { | |
295 | int policy = conf.policy; | |
296 | if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) { | |
297 | oss_logerr2 (errno, typ, "Failed to set timing policy to %d\n", | |
298 | conf.policy); | |
299 | goto err; | |
300 | } | |
301 | } | |
302 | else | |
303 | #endif | |
304 | { | |
305 | int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize); | |
306 | if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) { | |
307 | oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n", | |
308 | req->nfrags, req->fragsize); | |
309 | goto err; | |
310 | } | |
85571bc7 FB |
311 | } |
312 | ||
1d14ffa9 FB |
313 | if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) { |
314 | oss_logerr2 (errno, typ, "Failed to get buffer length\n"); | |
85571bc7 FB |
315 | goto err; |
316 | } | |
317 | ||
29ddf27b | 318 | if (!abinfo.fragstotal || !abinfo.fragsize) { |
319 | AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n", | |
320 | abinfo.fragstotal, abinfo.fragsize, typ); | |
321 | goto err; | |
322 | } | |
323 | ||
85571bc7 FB |
324 | obt->fmt = fmt; |
325 | obt->nchannels = nchannels; | |
326 | obt->freq = freq; | |
327 | obt->nfrags = abinfo.fragstotal; | |
328 | obt->fragsize = abinfo.fragsize; | |
329 | *pfd = fd; | |
330 | ||
c0fe3827 | 331 | #ifdef DEBUG_MISMATCHES |
85571bc7 FB |
332 | if ((req->fmt != obt->fmt) || |
333 | (req->nchannels != obt->nchannels) || | |
334 | (req->freq != obt->freq) || | |
335 | (req->fragsize != obt->fragsize) || | |
336 | (req->nfrags != obt->nfrags)) { | |
85571bc7 | 337 | dolog ("Audio parameters mismatch\n"); |
1d14ffa9 | 338 | oss_dump_info (req, obt); |
85571bc7 | 339 | } |
c0fe3827 | 340 | #endif |
85571bc7 | 341 | |
1d14ffa9 FB |
342 | #ifdef DEBUG |
343 | oss_dump_info (req, obt); | |
85571bc7 FB |
344 | #endif |
345 | return 0; | |
346 | ||
1d14ffa9 FB |
347 | err: |
348 | oss_anal_close (&fd); | |
85571bc7 FB |
349 | return -1; |
350 | } | |
351 | ||
9d168976 | 352 | static void oss_write_pending (OSSVoiceOut *oss) |
353 | { | |
354 | HWVoiceOut *hw = &oss->hw; | |
355 | ||
356 | if (oss->mmapped) { | |
357 | return; | |
358 | } | |
359 | ||
360 | while (oss->pending) { | |
361 | int samples_written; | |
362 | ssize_t bytes_written; | |
363 | int samples_till_end = hw->samples - oss->wpos; | |
364 | int samples_to_write = audio_MIN (oss->pending, samples_till_end); | |
365 | int bytes_to_write = samples_to_write << hw->info.shift; | |
366 | void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift); | |
367 | ||
368 | bytes_written = write (oss->fd, pcm, bytes_to_write); | |
369 | if (bytes_written < 0) { | |
370 | if (errno != EAGAIN) { | |
371 | oss_logerr (errno, "failed to write %d bytes\n", | |
372 | bytes_to_write); | |
373 | } | |
374 | break; | |
375 | } | |
376 | ||
377 | if (bytes_written & hw->info.align) { | |
378 | dolog ("misaligned write asked for %d, but got %zd\n", | |
379 | bytes_to_write, bytes_written); | |
380 | return; | |
381 | } | |
382 | ||
383 | samples_written = bytes_written >> hw->info.shift; | |
384 | oss->pending -= samples_written; | |
385 | oss->wpos = (oss->wpos + samples_written) % hw->samples; | |
386 | if (bytes_written - bytes_to_write) { | |
387 | break; | |
388 | } | |
389 | } | |
390 | } | |
391 | ||
bdff253c | 392 | static int oss_run_out (HWVoiceOut *hw, int live) |
85571bc7 | 393 | { |
1d14ffa9 | 394 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
bdff253c | 395 | int err, decr; |
85571bc7 FB |
396 | struct audio_buf_info abinfo; |
397 | struct count_info cntinfo; | |
c0fe3827 | 398 | int bufsize; |
85571bc7 | 399 | |
c0fe3827 FB |
400 | bufsize = hw->samples << hw->info.shift; |
401 | ||
85571bc7 | 402 | if (oss->mmapped) { |
54762b73 | 403 | int bytes, pos; |
85571bc7 FB |
404 | |
405 | err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); | |
406 | if (err < 0) { | |
1d14ffa9 FB |
407 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); |
408 | return 0; | |
85571bc7 FB |
409 | } |
410 | ||
54762b73 | 411 | pos = hw->rpos << hw->info.shift; |
412 | bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize); | |
1d14ffa9 | 413 | decr = audio_MIN (bytes >> hw->info.shift, live); |
85571bc7 FB |
414 | } |
415 | else { | |
416 | err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo); | |
417 | if (err < 0) { | |
1d14ffa9 FB |
418 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); |
419 | return 0; | |
420 | } | |
421 | ||
8ead62cf FB |
422 | if (abinfo.bytes > bufsize) { |
423 | if (conf.debug) { | |
424 | dolog ("warning: Invalid available size, size=%d bufsize=%d\n" | |
425 | "please report your OS/audio hw to malc@pulsesoft.com\n", | |
426 | abinfo.bytes, bufsize); | |
427 | } | |
428 | abinfo.bytes = bufsize; | |
429 | } | |
430 | ||
431 | if (abinfo.bytes < 0) { | |
432 | if (conf.debug) { | |
433 | dolog ("warning: Invalid available size, size=%d bufsize=%d\n", | |
434 | abinfo.bytes, bufsize); | |
435 | } | |
1d14ffa9 | 436 | return 0; |
85571bc7 FB |
437 | } |
438 | ||
1d14ffa9 FB |
439 | decr = audio_MIN (abinfo.bytes >> hw->info.shift, live); |
440 | if (!decr) { | |
441 | return 0; | |
442 | } | |
85571bc7 FB |
443 | } |
444 | ||
9d168976 | 445 | decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending); |
446 | oss->pending += decr; | |
447 | oss_write_pending (oss); | |
85571bc7 | 448 | |
1d14ffa9 | 449 | return decr; |
85571bc7 FB |
450 | } |
451 | ||
1d14ffa9 | 452 | static void oss_fini_out (HWVoiceOut *hw) |
85571bc7 FB |
453 | { |
454 | int err; | |
1d14ffa9 | 455 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 456 | |
1d14ffa9 FB |
457 | ldebug ("oss_fini\n"); |
458 | oss_anal_close (&oss->fd); | |
85571bc7 FB |
459 | |
460 | if (oss->pcm_buf) { | |
461 | if (oss->mmapped) { | |
c0fe3827 | 462 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
85571bc7 | 463 | if (err) { |
1d14ffa9 | 464 | oss_logerr (errno, "Failed to unmap buffer %p, size %d\n", |
c0fe3827 | 465 | oss->pcm_buf, hw->samples << hw->info.shift); |
85571bc7 FB |
466 | } |
467 | } | |
468 | else { | |
469 | qemu_free (oss->pcm_buf); | |
470 | } | |
471 | oss->pcm_buf = NULL; | |
472 | } | |
473 | } | |
474 | ||
1ea879e5 | 475 | static int oss_init_out (HWVoiceOut *hw, struct audsettings *as) |
85571bc7 | 476 | { |
1d14ffa9 | 477 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 478 | struct oss_params req, obt; |
1d14ffa9 FB |
479 | int endianness; |
480 | int err; | |
481 | int fd; | |
482 | audfmt_e effective_fmt; | |
1ea879e5 | 483 | struct audsettings obt_as; |
85571bc7 | 484 | |
571ec3d6 FB |
485 | oss->fd = -1; |
486 | ||
c0fe3827 FB |
487 | req.fmt = aud_to_ossfmt (as->fmt); |
488 | req.freq = as->freq; | |
489 | req.nchannels = as->nchannels; | |
85571bc7 FB |
490 | req.fragsize = conf.fragsize; |
491 | req.nfrags = conf.nfrags; | |
492 | ||
1d14ffa9 | 493 | if (oss_open (0, &req, &obt, &fd)) { |
85571bc7 | 494 | return -1; |
1d14ffa9 | 495 | } |
85571bc7 | 496 | |
1d14ffa9 FB |
497 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); |
498 | if (err) { | |
499 | oss_anal_close (&fd); | |
500 | return -1; | |
501 | } | |
85571bc7 | 502 | |
c0fe3827 FB |
503 | obt_as.freq = obt.freq; |
504 | obt_as.nchannels = obt.nchannels; | |
505 | obt_as.fmt = effective_fmt; | |
d929eba5 | 506 | obt_as.endianness = endianness; |
c0fe3827 | 507 | |
d929eba5 | 508 | audio_pcm_init_info (&hw->info, &obt_as); |
85571bc7 FB |
509 | oss->nfrags = obt.nfrags; |
510 | oss->fragsize = obt.fragsize; | |
c0fe3827 FB |
511 | |
512 | if (obt.nfrags * obt.fragsize & hw->info.align) { | |
513 | dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n", | |
514 | obt.nfrags * obt.fragsize, hw->info.align + 1); | |
515 | } | |
516 | ||
517 | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; | |
85571bc7 FB |
518 | |
519 | oss->mmapped = 0; | |
520 | if (conf.try_mmap) { | |
c0fe3827 | 521 | oss->pcm_buf = mmap ( |
660f11be | 522 | NULL, |
c0fe3827 FB |
523 | hw->samples << hw->info.shift, |
524 | PROT_READ | PROT_WRITE, | |
525 | MAP_SHARED, | |
526 | fd, | |
527 | 0 | |
528 | ); | |
85571bc7 | 529 | if (oss->pcm_buf == MAP_FAILED) { |
1d14ffa9 | 530 | oss_logerr (errno, "Failed to map %d bytes of DAC\n", |
c0fe3827 | 531 | hw->samples << hw->info.shift); |
54762b73 | 532 | } |
533 | else { | |
85571bc7 FB |
534 | int err; |
535 | int trig = 0; | |
1d14ffa9 FB |
536 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
537 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); | |
85571bc7 | 538 | } |
44a095a7 FB |
539 | else { |
540 | trig = PCM_ENABLE_OUTPUT; | |
1d14ffa9 FB |
541 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
542 | oss_logerr ( | |
543 | errno, | |
544 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
545 | ); | |
44a095a7 FB |
546 | } |
547 | else { | |
548 | oss->mmapped = 1; | |
549 | } | |
85571bc7 | 550 | } |
85571bc7 | 551 | |
44a095a7 | 552 | if (!oss->mmapped) { |
c0fe3827 | 553 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
44a095a7 | 554 | if (err) { |
1d14ffa9 | 555 | oss_logerr (errno, "Failed to unmap buffer %p size %d\n", |
c0fe3827 | 556 | oss->pcm_buf, hw->samples << hw->info.shift); |
44a095a7 | 557 | } |
85571bc7 FB |
558 | } |
559 | } | |
560 | } | |
561 | ||
562 | if (!oss->mmapped) { | |
c0fe3827 FB |
563 | oss->pcm_buf = audio_calloc ( |
564 | AUDIO_FUNC, | |
565 | hw->samples, | |
566 | 1 << hw->info.shift | |
567 | ); | |
85571bc7 | 568 | if (!oss->pcm_buf) { |
b41cffbe FB |
569 | dolog ( |
570 | "Could not allocate DAC buffer (%d samples, each %d bytes)\n", | |
571 | hw->samples, | |
572 | 1 << hw->info.shift | |
573 | ); | |
1d14ffa9 | 574 | oss_anal_close (&fd); |
85571bc7 FB |
575 | return -1; |
576 | } | |
577 | } | |
578 | ||
1d14ffa9 | 579 | oss->fd = fd; |
85571bc7 FB |
580 | return 0; |
581 | } | |
582 | ||
1d14ffa9 | 583 | static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...) |
85571bc7 FB |
584 | { |
585 | int trig; | |
dd8a5649 | 586 | va_list ap; |
587 | int poll_mode; | |
1d14ffa9 | 588 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 589 | |
dd8a5649 | 590 | va_start (ap, cmd); |
591 | poll_mode = va_arg (ap, int); | |
592 | va_end (ap); | |
85571bc7 FB |
593 | |
594 | switch (cmd) { | |
595 | case VOICE_ENABLE: | |
596 | ldebug ("enabling voice\n"); | |
dd8a5649 | 597 | if (poll_mode && oss_poll_out (hw)) { |
598 | poll_mode = 0; | |
599 | } | |
600 | hw->poll_mode = poll_mode; | |
601 | ||
602 | if (!oss->mmapped) { | |
603 | return 0; | |
604 | } | |
605 | ||
1d14ffa9 | 606 | audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples); |
85571bc7 FB |
607 | trig = PCM_ENABLE_OUTPUT; |
608 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
1d14ffa9 FB |
609 | oss_logerr ( |
610 | errno, | |
611 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
612 | ); | |
85571bc7 FB |
613 | return -1; |
614 | } | |
615 | break; | |
616 | ||
617 | case VOICE_DISABLE: | |
dd8a5649 | 618 | if (hw->poll_mode) { |
619 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
620 | hw->poll_mode = 0; | |
621 | } | |
622 | ||
623 | if (!oss->mmapped) { | |
624 | return 0; | |
625 | } | |
626 | ||
85571bc7 FB |
627 | ldebug ("disabling voice\n"); |
628 | trig = 0; | |
629 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
1d14ffa9 | 630 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); |
85571bc7 FB |
631 | return -1; |
632 | } | |
633 | break; | |
634 | } | |
635 | return 0; | |
636 | } | |
637 | ||
1ea879e5 | 638 | static int oss_init_in (HWVoiceIn *hw, struct audsettings *as) |
1d14ffa9 FB |
639 | { |
640 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
641 | struct oss_params req, obt; | |
642 | int endianness; | |
643 | int err; | |
644 | int fd; | |
645 | audfmt_e effective_fmt; | |
1ea879e5 | 646 | struct audsettings obt_as; |
1d14ffa9 | 647 | |
571ec3d6 FB |
648 | oss->fd = -1; |
649 | ||
c0fe3827 FB |
650 | req.fmt = aud_to_ossfmt (as->fmt); |
651 | req.freq = as->freq; | |
652 | req.nchannels = as->nchannels; | |
1d14ffa9 FB |
653 | req.fragsize = conf.fragsize; |
654 | req.nfrags = conf.nfrags; | |
655 | if (oss_open (1, &req, &obt, &fd)) { | |
656 | return -1; | |
657 | } | |
658 | ||
659 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); | |
660 | if (err) { | |
661 | oss_anal_close (&fd); | |
662 | return -1; | |
663 | } | |
664 | ||
c0fe3827 FB |
665 | obt_as.freq = obt.freq; |
666 | obt_as.nchannels = obt.nchannels; | |
667 | obt_as.fmt = effective_fmt; | |
d929eba5 | 668 | obt_as.endianness = endianness; |
c0fe3827 | 669 | |
d929eba5 | 670 | audio_pcm_init_info (&hw->info, &obt_as); |
1d14ffa9 FB |
671 | oss->nfrags = obt.nfrags; |
672 | oss->fragsize = obt.fragsize; | |
c0fe3827 FB |
673 | |
674 | if (obt.nfrags * obt.fragsize & hw->info.align) { | |
675 | dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n", | |
676 | obt.nfrags * obt.fragsize, hw->info.align + 1); | |
677 | } | |
678 | ||
679 | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; | |
680 | oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); | |
1d14ffa9 | 681 | if (!oss->pcm_buf) { |
b41cffbe FB |
682 | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n", |
683 | hw->samples, 1 << hw->info.shift); | |
1d14ffa9 FB |
684 | oss_anal_close (&fd); |
685 | return -1; | |
686 | } | |
687 | ||
688 | oss->fd = fd; | |
689 | return 0; | |
690 | } | |
691 | ||
692 | static void oss_fini_in (HWVoiceIn *hw) | |
693 | { | |
694 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
695 | ||
696 | oss_anal_close (&oss->fd); | |
697 | ||
698 | if (oss->pcm_buf) { | |
699 | qemu_free (oss->pcm_buf); | |
700 | oss->pcm_buf = NULL; | |
701 | } | |
702 | } | |
703 | ||
704 | static int oss_run_in (HWVoiceIn *hw) | |
705 | { | |
706 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
707 | int hwshift = hw->info.shift; | |
708 | int i; | |
709 | int live = audio_pcm_hw_get_live_in (hw); | |
710 | int dead = hw->samples - live; | |
711 | size_t read_samples = 0; | |
712 | struct { | |
713 | int add; | |
714 | int len; | |
715 | } bufs[2] = { | |
98f9f48c | 716 | { .add = hw->wpos, .len = 0 }, |
717 | { .add = 0, .len = 0 } | |
1d14ffa9 FB |
718 | }; |
719 | ||
720 | if (!dead) { | |
721 | return 0; | |
722 | } | |
723 | ||
724 | if (hw->wpos + dead > hw->samples) { | |
725 | bufs[0].len = (hw->samples - hw->wpos) << hwshift; | |
726 | bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift; | |
727 | } | |
728 | else { | |
729 | bufs[0].len = dead << hwshift; | |
730 | } | |
731 | ||
1d14ffa9 FB |
732 | for (i = 0; i < 2; ++i) { |
733 | ssize_t nread; | |
734 | ||
735 | if (bufs[i].len) { | |
736 | void *p = advance (oss->pcm_buf, bufs[i].add << hwshift); | |
737 | nread = read (oss->fd, p, bufs[i].len); | |
738 | ||
739 | if (nread > 0) { | |
740 | if (nread & hw->info.align) { | |
b41cffbe | 741 | dolog ("warning: Misaligned read %zd (requested %d), " |
1d14ffa9 FB |
742 | "alignment %d\n", nread, bufs[i].add << hwshift, |
743 | hw->info.align + 1); | |
744 | } | |
745 | read_samples += nread >> hwshift; | |
746 | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift, | |
747 | &nominal_volume); | |
748 | } | |
749 | ||
750 | if (bufs[i].len - nread) { | |
751 | if (nread == -1) { | |
752 | switch (errno) { | |
753 | case EINTR: | |
754 | case EAGAIN: | |
755 | break; | |
756 | default: | |
757 | oss_logerr ( | |
758 | errno, | |
759 | "Failed to read %d bytes of audio (to %p)\n", | |
760 | bufs[i].len, p | |
761 | ); | |
762 | break; | |
763 | } | |
764 | } | |
765 | break; | |
766 | } | |
767 | } | |
768 | } | |
769 | ||
770 | hw->wpos = (hw->wpos + read_samples) % hw->samples; | |
771 | return read_samples; | |
772 | } | |
773 | ||
774 | static int oss_read (SWVoiceIn *sw, void *buf, int size) | |
775 | { | |
776 | return audio_pcm_sw_read (sw, buf, size); | |
777 | } | |
778 | ||
779 | static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
780 | { | |
dd8a5649 | 781 | va_list ap; |
782 | int poll_mode; | |
783 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
784 | ||
785 | va_start (ap, cmd); | |
786 | poll_mode = va_arg (ap, int); | |
787 | va_end (ap); | |
788 | ||
789 | switch (cmd) { | |
790 | case VOICE_ENABLE: | |
791 | if (poll_mode && oss_poll_in (hw)) { | |
792 | poll_mode = 0; | |
793 | } | |
794 | hw->poll_mode = poll_mode; | |
795 | break; | |
796 | ||
797 | case VOICE_DISABLE: | |
798 | if (hw->poll_mode) { | |
799 | hw->poll_mode = 0; | |
800 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
801 | } | |
802 | break; | |
803 | } | |
1d14ffa9 FB |
804 | return 0; |
805 | } | |
806 | ||
85571bc7 FB |
807 | static void *oss_audio_init (void) |
808 | { | |
85571bc7 FB |
809 | return &conf; |
810 | } | |
811 | ||
812 | static void oss_audio_fini (void *opaque) | |
813 | { | |
1d14ffa9 | 814 | (void) opaque; |
85571bc7 FB |
815 | } |
816 | ||
1d14ffa9 | 817 | static struct audio_option oss_options[] = { |
98f9f48c | 818 | { |
819 | .name = "FRAGSIZE", | |
820 | .tag = AUD_OPT_INT, | |
821 | .valp = &conf.fragsize, | |
822 | .descr = "Fragment size in bytes" | |
823 | }, | |
824 | { | |
825 | .name = "NFRAGS", | |
826 | .tag = AUD_OPT_INT, | |
827 | .valp = &conf.nfrags, | |
828 | .descr = "Number of fragments" | |
829 | }, | |
830 | { | |
831 | .name = "MMAP", | |
832 | .tag = AUD_OPT_BOOL, | |
833 | .valp = &conf.try_mmap, | |
834 | .descr = "Try using memory mapped access" | |
835 | }, | |
836 | { | |
837 | .name = "DAC_DEV", | |
838 | .tag = AUD_OPT_STR, | |
839 | .valp = &conf.devpath_out, | |
840 | .descr = "Path to DAC device" | |
841 | }, | |
842 | { | |
843 | .name = "ADC_DEV", | |
844 | .tag = AUD_OPT_STR, | |
845 | .valp = &conf.devpath_in, | |
846 | .descr = "Path to ADC device" | |
847 | }, | |
0b3652bc | 848 | { |
849 | .name = "EXCLUSIVE", | |
850 | .tag = AUD_OPT_BOOL, | |
851 | .valp = &conf.exclusive, | |
852 | .descr = "Open device in exclusive mode (vmix wont work)" | |
853 | }, | |
854 | #ifdef SNDCTL_DSP_POLICY | |
855 | { | |
856 | .name = "POLICY", | |
857 | .tag = AUD_OPT_INT, | |
858 | .valp = &conf.policy, | |
859 | .descr = "Set the timing policy of the device, -1 to use fragment mode", | |
860 | }, | |
861 | #endif | |
98f9f48c | 862 | { |
863 | .name = "DEBUG", | |
864 | .tag = AUD_OPT_BOOL, | |
865 | .valp = &conf.debug, | |
866 | .descr = "Turn on some debugging messages" | |
867 | }, | |
2700efa3 | 868 | { /* End of list */ } |
1d14ffa9 FB |
869 | }; |
870 | ||
35f4b58c | 871 | static struct audio_pcm_ops oss_pcm_ops = { |
1dd3e4d1 JQ |
872 | .init_out = oss_init_out, |
873 | .fini_out = oss_fini_out, | |
874 | .run_out = oss_run_out, | |
875 | .write = oss_write, | |
876 | .ctl_out = oss_ctl_out, | |
877 | ||
878 | .init_in = oss_init_in, | |
879 | .fini_in = oss_fini_in, | |
880 | .run_in = oss_run_in, | |
881 | .read = oss_read, | |
882 | .ctl_in = oss_ctl_in | |
85571bc7 FB |
883 | }; |
884 | ||
1d14ffa9 | 885 | struct audio_driver oss_audio_driver = { |
bee37f32 JQ |
886 | .name = "oss", |
887 | .descr = "OSS http://www.opensound.com", | |
888 | .options = oss_options, | |
889 | .init = oss_audio_init, | |
890 | .fini = oss_audio_fini, | |
891 | .pcm_ops = &oss_pcm_ops, | |
892 | .can_be_default = 1, | |
893 | .max_voices_out = INT_MAX, | |
894 | .max_voices_in = INT_MAX, | |
895 | .voice_size_out = sizeof (OSSVoiceOut), | |
896 | .voice_size_in = sizeof (OSSVoiceIn) | |
85571bc7 | 897 | }; |