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