]>
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; | |
e726fe7d | 243 | #ifdef OSS_GETVERSION |
0b3652bc | 244 | int version; |
e726fe7d | 245 | #endif |
0b3652bc | 246 | int oflags = conf.exclusive ? O_EXCL : 0; |
85571bc7 FB |
247 | audio_buf_info abinfo; |
248 | int fmt, freq, nchannels; | |
1d14ffa9 FB |
249 | const char *dspname = in ? conf.devpath_in : conf.devpath_out; |
250 | const char *typ = in ? "ADC" : "DAC"; | |
85571bc7 | 251 | |
2182349d | 252 | /* Kludge needed to have working mmap on Linux */ |
0b3652bc | 253 | oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY); |
254 | ||
2182349d | 255 | fd = open (dspname, oflags | O_NONBLOCK); |
85571bc7 | 256 | if (-1 == fd) { |
1d14ffa9 | 257 | oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname); |
85571bc7 FB |
258 | return -1; |
259 | } | |
260 | ||
261 | freq = req->freq; | |
262 | nchannels = req->nchannels; | |
263 | fmt = req->fmt; | |
264 | ||
265 | if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) { | |
1d14ffa9 | 266 | oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt); |
85571bc7 FB |
267 | goto err; |
268 | } | |
269 | ||
270 | if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) { | |
1d14ffa9 FB |
271 | oss_logerr2 (errno, typ, "Failed to set number of channels %d\n", |
272 | req->nchannels); | |
85571bc7 FB |
273 | goto err; |
274 | } | |
275 | ||
276 | if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) { | |
1d14ffa9 | 277 | oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq); |
85571bc7 FB |
278 | goto err; |
279 | } | |
280 | ||
902e2b51 | 281 | if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) { |
1d14ffa9 | 282 | oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n"); |
85571bc7 FB |
283 | goto err; |
284 | } | |
285 | ||
e726fe7d | 286 | #ifdef OSS_GETVERSION |
0b3652bc | 287 | if (ioctl (fd, OSS_GETVERSION, &version)) { |
288 | oss_logerr2 (errno, typ, "Failed to get OSS version\n"); | |
289 | version = 0; | |
290 | } | |
291 | ||
292 | if (conf.debug) { | |
293 | dolog ("OSS version = %#x\n", version); | |
294 | } | |
e726fe7d | 295 | #endif |
0b3652bc | 296 | |
297 | #ifdef SNDCTL_DSP_POLICY | |
e726fe7d | 298 | if (conf.policy >= 0 |
299 | #ifdef OSS_GETVERSION | |
300 | && version >= 0x040000 | |
301 | #else | |
302 | 0 | |
303 | #endif | |
304 | ) | |
305 | { | |
0b3652bc | 306 | int policy = conf.policy; |
307 | if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) { | |
308 | oss_logerr2 (errno, typ, "Failed to set timing policy to %d\n", | |
309 | conf.policy); | |
310 | goto err; | |
311 | } | |
312 | } | |
313 | else | |
314 | #endif | |
315 | { | |
316 | int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize); | |
317 | if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) { | |
318 | oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n", | |
319 | req->nfrags, req->fragsize); | |
320 | goto err; | |
321 | } | |
85571bc7 FB |
322 | } |
323 | ||
1d14ffa9 FB |
324 | if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) { |
325 | oss_logerr2 (errno, typ, "Failed to get buffer length\n"); | |
85571bc7 FB |
326 | goto err; |
327 | } | |
328 | ||
29ddf27b | 329 | if (!abinfo.fragstotal || !abinfo.fragsize) { |
330 | AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n", | |
331 | abinfo.fragstotal, abinfo.fragsize, typ); | |
332 | goto err; | |
333 | } | |
334 | ||
85571bc7 FB |
335 | obt->fmt = fmt; |
336 | obt->nchannels = nchannels; | |
337 | obt->freq = freq; | |
338 | obt->nfrags = abinfo.fragstotal; | |
339 | obt->fragsize = abinfo.fragsize; | |
340 | *pfd = fd; | |
341 | ||
c0fe3827 | 342 | #ifdef DEBUG_MISMATCHES |
85571bc7 FB |
343 | if ((req->fmt != obt->fmt) || |
344 | (req->nchannels != obt->nchannels) || | |
345 | (req->freq != obt->freq) || | |
346 | (req->fragsize != obt->fragsize) || | |
347 | (req->nfrags != obt->nfrags)) { | |
85571bc7 | 348 | dolog ("Audio parameters mismatch\n"); |
1d14ffa9 | 349 | oss_dump_info (req, obt); |
85571bc7 | 350 | } |
c0fe3827 | 351 | #endif |
85571bc7 | 352 | |
1d14ffa9 FB |
353 | #ifdef DEBUG |
354 | oss_dump_info (req, obt); | |
85571bc7 FB |
355 | #endif |
356 | return 0; | |
357 | ||
1d14ffa9 FB |
358 | err: |
359 | oss_anal_close (&fd); | |
85571bc7 FB |
360 | return -1; |
361 | } | |
362 | ||
9d168976 | 363 | static void oss_write_pending (OSSVoiceOut *oss) |
364 | { | |
365 | HWVoiceOut *hw = &oss->hw; | |
366 | ||
367 | if (oss->mmapped) { | |
368 | return; | |
369 | } | |
370 | ||
371 | while (oss->pending) { | |
372 | int samples_written; | |
373 | ssize_t bytes_written; | |
374 | int samples_till_end = hw->samples - oss->wpos; | |
375 | int samples_to_write = audio_MIN (oss->pending, samples_till_end); | |
376 | int bytes_to_write = samples_to_write << hw->info.shift; | |
377 | void *pcm = advance (oss->pcm_buf, oss->wpos << hw->info.shift); | |
378 | ||
379 | bytes_written = write (oss->fd, pcm, bytes_to_write); | |
380 | if (bytes_written < 0) { | |
381 | if (errno != EAGAIN) { | |
382 | oss_logerr (errno, "failed to write %d bytes\n", | |
383 | bytes_to_write); | |
384 | } | |
385 | break; | |
386 | } | |
387 | ||
388 | if (bytes_written & hw->info.align) { | |
389 | dolog ("misaligned write asked for %d, but got %zd\n", | |
390 | bytes_to_write, bytes_written); | |
391 | return; | |
392 | } | |
393 | ||
394 | samples_written = bytes_written >> hw->info.shift; | |
395 | oss->pending -= samples_written; | |
396 | oss->wpos = (oss->wpos + samples_written) % hw->samples; | |
397 | if (bytes_written - bytes_to_write) { | |
398 | break; | |
399 | } | |
400 | } | |
401 | } | |
402 | ||
bdff253c | 403 | static int oss_run_out (HWVoiceOut *hw, int live) |
85571bc7 | 404 | { |
1d14ffa9 | 405 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
bdff253c | 406 | int err, decr; |
85571bc7 FB |
407 | struct audio_buf_info abinfo; |
408 | struct count_info cntinfo; | |
c0fe3827 | 409 | int bufsize; |
85571bc7 | 410 | |
c0fe3827 FB |
411 | bufsize = hw->samples << hw->info.shift; |
412 | ||
85571bc7 | 413 | if (oss->mmapped) { |
54762b73 | 414 | int bytes, pos; |
85571bc7 FB |
415 | |
416 | err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); | |
417 | if (err < 0) { | |
1d14ffa9 FB |
418 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); |
419 | return 0; | |
85571bc7 FB |
420 | } |
421 | ||
54762b73 | 422 | pos = hw->rpos << hw->info.shift; |
423 | bytes = audio_ring_dist (cntinfo.ptr, pos, bufsize); | |
1d14ffa9 | 424 | decr = audio_MIN (bytes >> hw->info.shift, live); |
85571bc7 FB |
425 | } |
426 | else { | |
427 | err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo); | |
428 | if (err < 0) { | |
1d14ffa9 FB |
429 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); |
430 | return 0; | |
431 | } | |
432 | ||
8ead62cf FB |
433 | if (abinfo.bytes > bufsize) { |
434 | if (conf.debug) { | |
435 | dolog ("warning: Invalid available size, size=%d bufsize=%d\n" | |
155a8ad3 | 436 | "please report your OS/audio hw to av1474@comtv.ru\n", |
8ead62cf FB |
437 | abinfo.bytes, bufsize); |
438 | } | |
439 | abinfo.bytes = bufsize; | |
440 | } | |
441 | ||
442 | if (abinfo.bytes < 0) { | |
443 | if (conf.debug) { | |
444 | dolog ("warning: Invalid available size, size=%d bufsize=%d\n", | |
445 | abinfo.bytes, bufsize); | |
446 | } | |
1d14ffa9 | 447 | return 0; |
85571bc7 FB |
448 | } |
449 | ||
1d14ffa9 FB |
450 | decr = audio_MIN (abinfo.bytes >> hw->info.shift, live); |
451 | if (!decr) { | |
452 | return 0; | |
453 | } | |
85571bc7 FB |
454 | } |
455 | ||
9d168976 | 456 | decr = audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending); |
457 | oss->pending += decr; | |
458 | oss_write_pending (oss); | |
85571bc7 | 459 | |
1d14ffa9 | 460 | return decr; |
85571bc7 FB |
461 | } |
462 | ||
1d14ffa9 | 463 | static void oss_fini_out (HWVoiceOut *hw) |
85571bc7 FB |
464 | { |
465 | int err; | |
1d14ffa9 | 466 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 467 | |
1d14ffa9 FB |
468 | ldebug ("oss_fini\n"); |
469 | oss_anal_close (&oss->fd); | |
85571bc7 FB |
470 | |
471 | if (oss->pcm_buf) { | |
472 | if (oss->mmapped) { | |
c0fe3827 | 473 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
85571bc7 | 474 | if (err) { |
1d14ffa9 | 475 | oss_logerr (errno, "Failed to unmap buffer %p, size %d\n", |
c0fe3827 | 476 | oss->pcm_buf, hw->samples << hw->info.shift); |
85571bc7 FB |
477 | } |
478 | } | |
479 | else { | |
480 | qemu_free (oss->pcm_buf); | |
481 | } | |
482 | oss->pcm_buf = NULL; | |
483 | } | |
484 | } | |
485 | ||
1ea879e5 | 486 | static int oss_init_out (HWVoiceOut *hw, struct audsettings *as) |
85571bc7 | 487 | { |
1d14ffa9 | 488 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 489 | struct oss_params req, obt; |
1d14ffa9 FB |
490 | int endianness; |
491 | int err; | |
492 | int fd; | |
493 | audfmt_e effective_fmt; | |
1ea879e5 | 494 | struct audsettings obt_as; |
85571bc7 | 495 | |
571ec3d6 FB |
496 | oss->fd = -1; |
497 | ||
c0fe3827 FB |
498 | req.fmt = aud_to_ossfmt (as->fmt); |
499 | req.freq = as->freq; | |
500 | req.nchannels = as->nchannels; | |
85571bc7 FB |
501 | req.fragsize = conf.fragsize; |
502 | req.nfrags = conf.nfrags; | |
503 | ||
1d14ffa9 | 504 | if (oss_open (0, &req, &obt, &fd)) { |
85571bc7 | 505 | return -1; |
1d14ffa9 | 506 | } |
85571bc7 | 507 | |
1d14ffa9 FB |
508 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); |
509 | if (err) { | |
510 | oss_anal_close (&fd); | |
511 | return -1; | |
512 | } | |
85571bc7 | 513 | |
c0fe3827 FB |
514 | obt_as.freq = obt.freq; |
515 | obt_as.nchannels = obt.nchannels; | |
516 | obt_as.fmt = effective_fmt; | |
d929eba5 | 517 | obt_as.endianness = endianness; |
c0fe3827 | 518 | |
d929eba5 | 519 | audio_pcm_init_info (&hw->info, &obt_as); |
85571bc7 FB |
520 | oss->nfrags = obt.nfrags; |
521 | oss->fragsize = obt.fragsize; | |
c0fe3827 FB |
522 | |
523 | if (obt.nfrags * obt.fragsize & hw->info.align) { | |
524 | dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n", | |
525 | obt.nfrags * obt.fragsize, hw->info.align + 1); | |
526 | } | |
527 | ||
528 | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; | |
85571bc7 FB |
529 | |
530 | oss->mmapped = 0; | |
531 | if (conf.try_mmap) { | |
c0fe3827 | 532 | oss->pcm_buf = mmap ( |
660f11be | 533 | NULL, |
c0fe3827 FB |
534 | hw->samples << hw->info.shift, |
535 | PROT_READ | PROT_WRITE, | |
536 | MAP_SHARED, | |
537 | fd, | |
538 | 0 | |
539 | ); | |
85571bc7 | 540 | if (oss->pcm_buf == MAP_FAILED) { |
1d14ffa9 | 541 | oss_logerr (errno, "Failed to map %d bytes of DAC\n", |
c0fe3827 | 542 | hw->samples << hw->info.shift); |
54762b73 | 543 | } |
544 | else { | |
85571bc7 FB |
545 | int err; |
546 | int trig = 0; | |
1d14ffa9 FB |
547 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
548 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); | |
85571bc7 | 549 | } |
44a095a7 FB |
550 | else { |
551 | trig = PCM_ENABLE_OUTPUT; | |
1d14ffa9 FB |
552 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
553 | oss_logerr ( | |
554 | errno, | |
555 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
556 | ); | |
44a095a7 FB |
557 | } |
558 | else { | |
559 | oss->mmapped = 1; | |
560 | } | |
85571bc7 | 561 | } |
85571bc7 | 562 | |
44a095a7 | 563 | if (!oss->mmapped) { |
c0fe3827 | 564 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); |
44a095a7 | 565 | if (err) { |
1d14ffa9 | 566 | oss_logerr (errno, "Failed to unmap buffer %p size %d\n", |
c0fe3827 | 567 | oss->pcm_buf, hw->samples << hw->info.shift); |
44a095a7 | 568 | } |
85571bc7 FB |
569 | } |
570 | } | |
571 | } | |
572 | ||
573 | if (!oss->mmapped) { | |
c0fe3827 FB |
574 | oss->pcm_buf = audio_calloc ( |
575 | AUDIO_FUNC, | |
576 | hw->samples, | |
577 | 1 << hw->info.shift | |
578 | ); | |
85571bc7 | 579 | if (!oss->pcm_buf) { |
b41cffbe FB |
580 | dolog ( |
581 | "Could not allocate DAC buffer (%d samples, each %d bytes)\n", | |
582 | hw->samples, | |
583 | 1 << hw->info.shift | |
584 | ); | |
1d14ffa9 | 585 | oss_anal_close (&fd); |
85571bc7 FB |
586 | return -1; |
587 | } | |
588 | } | |
589 | ||
1d14ffa9 | 590 | oss->fd = fd; |
85571bc7 FB |
591 | return 0; |
592 | } | |
593 | ||
1d14ffa9 | 594 | static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...) |
85571bc7 FB |
595 | { |
596 | int trig; | |
1d14ffa9 | 597 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 598 | |
85571bc7 FB |
599 | switch (cmd) { |
600 | case VOICE_ENABLE: | |
301901b5 | 601 | { |
602 | va_list ap; | |
603 | int poll_mode; | |
dd8a5649 | 604 | |
301901b5 | 605 | va_start (ap, cmd); |
606 | poll_mode = va_arg (ap, int); | |
607 | va_end (ap); | |
dd8a5649 | 608 | |
301901b5 | 609 | ldebug ("enabling voice\n"); |
610 | if (poll_mode && oss_poll_out (hw)) { | |
611 | poll_mode = 0; | |
612 | } | |
613 | hw->poll_mode = poll_mode; | |
614 | ||
615 | if (!oss->mmapped) { | |
616 | return 0; | |
617 | } | |
618 | ||
619 | audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples); | |
620 | trig = PCM_ENABLE_OUTPUT; | |
621 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
622 | oss_logerr ( | |
623 | errno, | |
624 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
625 | ); | |
626 | return -1; | |
627 | } | |
85571bc7 FB |
628 | } |
629 | break; | |
630 | ||
631 | case VOICE_DISABLE: | |
dd8a5649 | 632 | if (hw->poll_mode) { |
633 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
634 | hw->poll_mode = 0; | |
635 | } | |
636 | ||
637 | if (!oss->mmapped) { | |
638 | return 0; | |
639 | } | |
640 | ||
85571bc7 FB |
641 | ldebug ("disabling voice\n"); |
642 | trig = 0; | |
643 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
1d14ffa9 | 644 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); |
85571bc7 FB |
645 | return -1; |
646 | } | |
647 | break; | |
648 | } | |
649 | return 0; | |
650 | } | |
651 | ||
1ea879e5 | 652 | static int oss_init_in (HWVoiceIn *hw, struct audsettings *as) |
1d14ffa9 FB |
653 | { |
654 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
655 | struct oss_params req, obt; | |
656 | int endianness; | |
657 | int err; | |
658 | int fd; | |
659 | audfmt_e effective_fmt; | |
1ea879e5 | 660 | struct audsettings obt_as; |
1d14ffa9 | 661 | |
571ec3d6 FB |
662 | oss->fd = -1; |
663 | ||
c0fe3827 FB |
664 | req.fmt = aud_to_ossfmt (as->fmt); |
665 | req.freq = as->freq; | |
666 | req.nchannels = as->nchannels; | |
1d14ffa9 FB |
667 | req.fragsize = conf.fragsize; |
668 | req.nfrags = conf.nfrags; | |
669 | if (oss_open (1, &req, &obt, &fd)) { | |
670 | return -1; | |
671 | } | |
672 | ||
673 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); | |
674 | if (err) { | |
675 | oss_anal_close (&fd); | |
676 | return -1; | |
677 | } | |
678 | ||
c0fe3827 FB |
679 | obt_as.freq = obt.freq; |
680 | obt_as.nchannels = obt.nchannels; | |
681 | obt_as.fmt = effective_fmt; | |
d929eba5 | 682 | obt_as.endianness = endianness; |
c0fe3827 | 683 | |
d929eba5 | 684 | audio_pcm_init_info (&hw->info, &obt_as); |
1d14ffa9 FB |
685 | oss->nfrags = obt.nfrags; |
686 | oss->fragsize = obt.fragsize; | |
c0fe3827 FB |
687 | |
688 | if (obt.nfrags * obt.fragsize & hw->info.align) { | |
689 | dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n", | |
690 | obt.nfrags * obt.fragsize, hw->info.align + 1); | |
691 | } | |
692 | ||
693 | hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; | |
694 | oss->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift); | |
1d14ffa9 | 695 | if (!oss->pcm_buf) { |
b41cffbe FB |
696 | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n", |
697 | hw->samples, 1 << hw->info.shift); | |
1d14ffa9 FB |
698 | oss_anal_close (&fd); |
699 | return -1; | |
700 | } | |
701 | ||
702 | oss->fd = fd; | |
703 | return 0; | |
704 | } | |
705 | ||
706 | static void oss_fini_in (HWVoiceIn *hw) | |
707 | { | |
708 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
709 | ||
710 | oss_anal_close (&oss->fd); | |
711 | ||
712 | if (oss->pcm_buf) { | |
713 | qemu_free (oss->pcm_buf); | |
714 | oss->pcm_buf = NULL; | |
715 | } | |
716 | } | |
717 | ||
718 | static int oss_run_in (HWVoiceIn *hw) | |
719 | { | |
720 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
721 | int hwshift = hw->info.shift; | |
722 | int i; | |
723 | int live = audio_pcm_hw_get_live_in (hw); | |
724 | int dead = hw->samples - live; | |
725 | size_t read_samples = 0; | |
726 | struct { | |
727 | int add; | |
728 | int len; | |
729 | } bufs[2] = { | |
98f9f48c | 730 | { .add = hw->wpos, .len = 0 }, |
731 | { .add = 0, .len = 0 } | |
1d14ffa9 FB |
732 | }; |
733 | ||
734 | if (!dead) { | |
735 | return 0; | |
736 | } | |
737 | ||
738 | if (hw->wpos + dead > hw->samples) { | |
739 | bufs[0].len = (hw->samples - hw->wpos) << hwshift; | |
740 | bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift; | |
741 | } | |
742 | else { | |
743 | bufs[0].len = dead << hwshift; | |
744 | } | |
745 | ||
1d14ffa9 FB |
746 | for (i = 0; i < 2; ++i) { |
747 | ssize_t nread; | |
748 | ||
749 | if (bufs[i].len) { | |
750 | void *p = advance (oss->pcm_buf, bufs[i].add << hwshift); | |
751 | nread = read (oss->fd, p, bufs[i].len); | |
752 | ||
753 | if (nread > 0) { | |
754 | if (nread & hw->info.align) { | |
b41cffbe | 755 | dolog ("warning: Misaligned read %zd (requested %d), " |
1d14ffa9 FB |
756 | "alignment %d\n", nread, bufs[i].add << hwshift, |
757 | hw->info.align + 1); | |
758 | } | |
759 | read_samples += nread >> hwshift; | |
760 | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift, | |
761 | &nominal_volume); | |
762 | } | |
763 | ||
764 | if (bufs[i].len - nread) { | |
765 | if (nread == -1) { | |
766 | switch (errno) { | |
767 | case EINTR: | |
768 | case EAGAIN: | |
769 | break; | |
770 | default: | |
771 | oss_logerr ( | |
772 | errno, | |
773 | "Failed to read %d bytes of audio (to %p)\n", | |
774 | bufs[i].len, p | |
775 | ); | |
776 | break; | |
777 | } | |
778 | } | |
779 | break; | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | hw->wpos = (hw->wpos + read_samples) % hw->samples; | |
785 | return read_samples; | |
786 | } | |
787 | ||
788 | static int oss_read (SWVoiceIn *sw, void *buf, int size) | |
789 | { | |
790 | return audio_pcm_sw_read (sw, buf, size); | |
791 | } | |
792 | ||
793 | static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
794 | { | |
dd8a5649 | 795 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
796 | ||
dd8a5649 | 797 | switch (cmd) { |
798 | case VOICE_ENABLE: | |
a628b869 | 799 | { |
800 | va_list ap; | |
801 | int poll_mode; | |
802 | ||
803 | va_start (ap, cmd); | |
804 | poll_mode = va_arg (ap, int); | |
805 | va_end (ap); | |
806 | ||
807 | if (poll_mode && oss_poll_in (hw)) { | |
808 | poll_mode = 0; | |
809 | } | |
810 | hw->poll_mode = poll_mode; | |
dd8a5649 | 811 | } |
dd8a5649 | 812 | break; |
813 | ||
814 | case VOICE_DISABLE: | |
815 | if (hw->poll_mode) { | |
816 | hw->poll_mode = 0; | |
817 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
818 | } | |
819 | break; | |
820 | } | |
1d14ffa9 FB |
821 | return 0; |
822 | } | |
823 | ||
85571bc7 FB |
824 | static void *oss_audio_init (void) |
825 | { | |
85571bc7 FB |
826 | return &conf; |
827 | } | |
828 | ||
829 | static void oss_audio_fini (void *opaque) | |
830 | { | |
1d14ffa9 | 831 | (void) opaque; |
85571bc7 FB |
832 | } |
833 | ||
1d14ffa9 | 834 | static struct audio_option oss_options[] = { |
98f9f48c | 835 | { |
836 | .name = "FRAGSIZE", | |
837 | .tag = AUD_OPT_INT, | |
838 | .valp = &conf.fragsize, | |
839 | .descr = "Fragment size in bytes" | |
840 | }, | |
841 | { | |
842 | .name = "NFRAGS", | |
843 | .tag = AUD_OPT_INT, | |
844 | .valp = &conf.nfrags, | |
845 | .descr = "Number of fragments" | |
846 | }, | |
847 | { | |
848 | .name = "MMAP", | |
849 | .tag = AUD_OPT_BOOL, | |
850 | .valp = &conf.try_mmap, | |
851 | .descr = "Try using memory mapped access" | |
852 | }, | |
853 | { | |
854 | .name = "DAC_DEV", | |
855 | .tag = AUD_OPT_STR, | |
856 | .valp = &conf.devpath_out, | |
857 | .descr = "Path to DAC device" | |
858 | }, | |
859 | { | |
860 | .name = "ADC_DEV", | |
861 | .tag = AUD_OPT_STR, | |
862 | .valp = &conf.devpath_in, | |
863 | .descr = "Path to ADC device" | |
864 | }, | |
0b3652bc | 865 | { |
866 | .name = "EXCLUSIVE", | |
867 | .tag = AUD_OPT_BOOL, | |
868 | .valp = &conf.exclusive, | |
869 | .descr = "Open device in exclusive mode (vmix wont work)" | |
870 | }, | |
871 | #ifdef SNDCTL_DSP_POLICY | |
872 | { | |
873 | .name = "POLICY", | |
874 | .tag = AUD_OPT_INT, | |
875 | .valp = &conf.policy, | |
876 | .descr = "Set the timing policy of the device, -1 to use fragment mode", | |
877 | }, | |
878 | #endif | |
98f9f48c | 879 | { |
880 | .name = "DEBUG", | |
881 | .tag = AUD_OPT_BOOL, | |
882 | .valp = &conf.debug, | |
883 | .descr = "Turn on some debugging messages" | |
884 | }, | |
2700efa3 | 885 | { /* End of list */ } |
1d14ffa9 FB |
886 | }; |
887 | ||
35f4b58c | 888 | static struct audio_pcm_ops oss_pcm_ops = { |
1dd3e4d1 JQ |
889 | .init_out = oss_init_out, |
890 | .fini_out = oss_fini_out, | |
891 | .run_out = oss_run_out, | |
892 | .write = oss_write, | |
893 | .ctl_out = oss_ctl_out, | |
894 | ||
895 | .init_in = oss_init_in, | |
896 | .fini_in = oss_fini_in, | |
897 | .run_in = oss_run_in, | |
898 | .read = oss_read, | |
899 | .ctl_in = oss_ctl_in | |
85571bc7 FB |
900 | }; |
901 | ||
1d14ffa9 | 902 | struct audio_driver oss_audio_driver = { |
bee37f32 JQ |
903 | .name = "oss", |
904 | .descr = "OSS http://www.opensound.com", | |
905 | .options = oss_options, | |
906 | .init = oss_audio_init, | |
907 | .fini = oss_audio_fini, | |
908 | .pcm_ops = &oss_pcm_ops, | |
909 | .can_be_default = 1, | |
910 | .max_voices_out = INT_MAX, | |
911 | .max_voices_in = INT_MAX, | |
912 | .voice_size_out = sizeof (OSSVoiceOut), | |
913 | .voice_size_in = sizeof (OSSVoiceIn) | |
85571bc7 | 914 | }; |