]>
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 | */ | |
0b8fa32f | 24 | |
6086a565 | 25 | #include "qemu/osdep.h" |
85571bc7 FB |
26 | #include <sys/ioctl.h> |
27 | #include <sys/soundcard.h> | |
1de7afc9 | 28 | #include "qemu/main-loop.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
1de7afc9 | 30 | #include "qemu/host-utils.h" |
87ecb68b | 31 | #include "audio.h" |
d95d7d80 | 32 | #include "trace.h" |
fb065187 | 33 | |
1d14ffa9 FB |
34 | #define AUDIO_CAP "oss" |
35 | #include "audio_int.h" | |
fb065187 | 36 | |
78d9356d | 37 | #if defined OSS_GETVERSION && defined SNDCTL_DSP_POLICY |
38 | #define USE_DSP_POLICY | |
39 | #endif | |
40 | ||
1d14ffa9 FB |
41 | typedef struct OSSVoiceOut { |
42 | HWVoiceOut hw; | |
fb065187 FB |
43 | int fd; |
44 | int nfrags; | |
45 | int fragsize; | |
46 | int mmapped; | |
baf6c7f4 | 47 | Audiodev *dev; |
1d14ffa9 | 48 | } OSSVoiceOut; |
85571bc7 | 49 | |
1d14ffa9 FB |
50 | typedef struct OSSVoiceIn { |
51 | HWVoiceIn hw; | |
1d14ffa9 FB |
52 | int fd; |
53 | int nfrags; | |
54 | int fragsize; | |
baf6c7f4 | 55 | Audiodev *dev; |
1d14ffa9 | 56 | } OSSVoiceIn; |
85571bc7 | 57 | |
85571bc7 FB |
58 | struct oss_params { |
59 | int freq; | |
baf6c7f4 | 60 | int fmt; |
85571bc7 FB |
61 | int nchannels; |
62 | int nfrags; | |
63 | int fragsize; | |
64 | }; | |
65 | ||
1d14ffa9 | 66 | static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...) |
85571bc7 | 67 | { |
1d14ffa9 FB |
68 | va_list ap; |
69 | ||
571ec3d6 | 70 | va_start (ap, fmt); |
1d14ffa9 | 71 | AUD_vlog (AUDIO_CAP, fmt, ap); |
571ec3d6 | 72 | va_end (ap); |
1d14ffa9 | 73 | |
1d14ffa9 | 74 | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err)); |
85571bc7 FB |
75 | } |
76 | ||
1d14ffa9 FB |
77 | static void GCC_FMT_ATTR (3, 4) oss_logerr2 ( |
78 | int err, | |
79 | const char *typ, | |
80 | const char *fmt, | |
81 | ... | |
82 | ) | |
83 | { | |
84 | va_list ap; | |
85 | ||
c0fe3827 | 86 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
87 | |
88 | va_start (ap, fmt); | |
89 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
90 | va_end (ap); | |
91 | ||
92 | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err)); | |
93 | } | |
94 | ||
95 | static void oss_anal_close (int *fdp) | |
96 | { | |
6ebfda13 | 97 | int err; |
98 | ||
99 | qemu_set_fd_handler (*fdp, NULL, NULL, NULL); | |
100 | err = close (*fdp); | |
1d14ffa9 FB |
101 | if (err) { |
102 | oss_logerr (errno, "Failed to close file(fd=%d)\n", *fdp); | |
103 | } | |
104 | *fdp = -1; | |
105 | } | |
106 | ||
dd8a5649 | 107 | static void oss_helper_poll_out (void *opaque) |
108 | { | |
18e2c177 KZ |
109 | AudioState *s = opaque; |
110 | audio_run(s, "oss_poll_out"); | |
dd8a5649 | 111 | } |
112 | ||
113 | static void oss_helper_poll_in (void *opaque) | |
114 | { | |
18e2c177 KZ |
115 | AudioState *s = opaque; |
116 | audio_run(s, "oss_poll_in"); | |
dd8a5649 | 117 | } |
118 | ||
b027a538 | 119 | static void oss_poll_out (HWVoiceOut *hw) |
dd8a5649 | 120 | { |
121 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
122 | ||
18e2c177 | 123 | qemu_set_fd_handler(oss->fd, NULL, oss_helper_poll_out, hw->s); |
dd8a5649 | 124 | } |
125 | ||
b027a538 | 126 | static void oss_poll_in (HWVoiceIn *hw) |
dd8a5649 | 127 | { |
128 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
129 | ||
18e2c177 | 130 | qemu_set_fd_handler(oss->fd, oss_helper_poll_in, NULL, hw->s); |
dd8a5649 | 131 | } |
132 | ||
85bc5852 | 133 | static int aud_to_ossfmt (AudioFormat fmt, int endianness) |
85571bc7 FB |
134 | { |
135 | switch (fmt) { | |
85bc5852 | 136 | case AUDIO_FORMAT_S8: |
1d14ffa9 FB |
137 | return AFMT_S8; |
138 | ||
85bc5852 | 139 | case AUDIO_FORMAT_U8: |
1d14ffa9 FB |
140 | return AFMT_U8; |
141 | ||
85bc5852 | 142 | case AUDIO_FORMAT_S16: |
b6c9c940 MW |
143 | if (endianness) { |
144 | return AFMT_S16_BE; | |
145 | } | |
146 | else { | |
147 | return AFMT_S16_LE; | |
148 | } | |
1d14ffa9 | 149 | |
85bc5852 | 150 | case AUDIO_FORMAT_U16: |
b6c9c940 MW |
151 | if (endianness) { |
152 | return AFMT_U16_BE; | |
153 | } | |
154 | else { | |
155 | return AFMT_U16_LE; | |
156 | } | |
1d14ffa9 | 157 | |
85571bc7 | 158 | default: |
1d14ffa9 FB |
159 | dolog ("Internal logic error: Bad audio format %d\n", fmt); |
160 | #ifdef DEBUG_AUDIO | |
161 | abort (); | |
162 | #endif | |
163 | return AFMT_U8; | |
85571bc7 FB |
164 | } |
165 | } | |
166 | ||
85bc5852 | 167 | static int oss_to_audfmt (int ossfmt, AudioFormat *fmt, int *endianness) |
85571bc7 | 168 | { |
1d14ffa9 FB |
169 | switch (ossfmt) { |
170 | case AFMT_S8: | |
ca9cc28c | 171 | *endianness = 0; |
85bc5852 | 172 | *fmt = AUDIO_FORMAT_S8; |
1d14ffa9 FB |
173 | break; |
174 | ||
175 | case AFMT_U8: | |
176 | *endianness = 0; | |
85bc5852 | 177 | *fmt = AUDIO_FORMAT_U8; |
1d14ffa9 FB |
178 | break; |
179 | ||
180 | case AFMT_S16_LE: | |
181 | *endianness = 0; | |
85bc5852 | 182 | *fmt = AUDIO_FORMAT_S16; |
1d14ffa9 FB |
183 | break; |
184 | ||
185 | case AFMT_U16_LE: | |
186 | *endianness = 0; | |
85bc5852 | 187 | *fmt = AUDIO_FORMAT_U16; |
1d14ffa9 FB |
188 | break; |
189 | ||
190 | case AFMT_S16_BE: | |
191 | *endianness = 1; | |
85bc5852 | 192 | *fmt = AUDIO_FORMAT_S16; |
1d14ffa9 FB |
193 | break; |
194 | ||
195 | case AFMT_U16_BE: | |
196 | *endianness = 1; | |
85bc5852 | 197 | *fmt = AUDIO_FORMAT_U16; |
1d14ffa9 FB |
198 | break; |
199 | ||
85571bc7 | 200 | default: |
1d14ffa9 FB |
201 | dolog ("Unrecognized audio format %d\n", ossfmt); |
202 | return -1; | |
85571bc7 | 203 | } |
1d14ffa9 FB |
204 | |
205 | return 0; | |
85571bc7 FB |
206 | } |
207 | ||
c0fe3827 | 208 | #if defined DEBUG_MISMATCHES || defined DEBUG |
1d14ffa9 | 209 | static void oss_dump_info (struct oss_params *req, struct oss_params *obt) |
85571bc7 FB |
210 | { |
211 | dolog ("parameter | requested value | obtained value\n"); | |
212 | dolog ("format | %10d | %10d\n", req->fmt, obt->fmt); | |
1d14ffa9 FB |
213 | dolog ("channels | %10d | %10d\n", |
214 | req->nchannels, obt->nchannels); | |
85571bc7 FB |
215 | dolog ("frequency | %10d | %10d\n", req->freq, obt->freq); |
216 | dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags); | |
1d14ffa9 FB |
217 | dolog ("fragsize | %10d | %10d\n", |
218 | req->fragsize, obt->fragsize); | |
85571bc7 FB |
219 | } |
220 | #endif | |
221 | ||
72ff25e4 JL |
222 | #ifdef USE_DSP_POLICY |
223 | static int oss_get_version (int fd, int *version, const char *typ) | |
224 | { | |
225 | if (ioctl (fd, OSS_GETVERSION, &version)) { | |
226 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) | |
227 | /* | |
228 | * Looks like atm (20100109) FreeBSD knows OSS_GETVERSION | |
229 | * since 7.x, but currently only on the mixer device (or in | |
230 | * the Linuxolator), and in the native version that part of | |
231 | * the code is in fact never reached so the ioctl fails anyway. | |
232 | * Until this is fixed, just check the errno and if its what | |
233 | * FreeBSD's sound drivers return atm assume they are new enough. | |
234 | */ | |
235 | if (errno == EINVAL) { | |
236 | *version = 0x040000; | |
237 | return 0; | |
238 | } | |
239 | #endif | |
240 | oss_logerr2 (errno, typ, "Failed to get OSS version\n"); | |
241 | return -1; | |
242 | } | |
243 | return 0; | |
244 | } | |
245 | #endif | |
246 | ||
baf6c7f4 KZ |
247 | static int oss_open(int in, struct oss_params *req, audsettings *as, |
248 | struct oss_params *obt, int *pfd, Audiodev *dev) | |
85571bc7 | 249 | { |
baf6c7f4 KZ |
250 | AudiodevOssOptions *oopts = &dev->u.oss; |
251 | AudiodevOssPerDirectionOptions *opdo = in ? oopts->in : oopts->out; | |
85571bc7 | 252 | int fd; |
baf6c7f4 | 253 | int oflags = (oopts->has_exclusive && oopts->exclusive) ? O_EXCL : 0; |
85571bc7 FB |
254 | audio_buf_info abinfo; |
255 | int fmt, freq, nchannels; | |
3d709fe7 | 256 | int setfragment = 1; |
baf6c7f4 | 257 | const char *dspname = opdo->has_dev ? opdo->dev : "/dev/dsp"; |
1d14ffa9 | 258 | const char *typ = in ? "ADC" : "DAC"; |
baf6c7f4 KZ |
259 | #ifdef USE_DSP_POLICY |
260 | int policy = oopts->has_dsp_policy ? oopts->dsp_policy : 5; | |
261 | #endif | |
85571bc7 | 262 | |
2182349d | 263 | /* Kludge needed to have working mmap on Linux */ |
baf6c7f4 KZ |
264 | oflags |= (oopts->has_try_mmap && oopts->try_mmap) ? |
265 | O_RDWR : (in ? O_RDONLY : O_WRONLY); | |
0b3652bc | 266 | |
2182349d | 267 | fd = open (dspname, oflags | O_NONBLOCK); |
85571bc7 | 268 | if (-1 == fd) { |
1d14ffa9 | 269 | oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname); |
85571bc7 FB |
270 | return -1; |
271 | } | |
272 | ||
273 | freq = req->freq; | |
274 | nchannels = req->nchannels; | |
275 | fmt = req->fmt; | |
baf6c7f4 KZ |
276 | req->nfrags = opdo->has_buffer_count ? opdo->buffer_count : 4; |
277 | req->fragsize = audio_buffer_bytes( | |
278 | qapi_AudiodevOssPerDirectionOptions_base(opdo), as, 23220); | |
85571bc7 FB |
279 | |
280 | if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) { | |
1d14ffa9 | 281 | oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt); |
85571bc7 FB |
282 | goto err; |
283 | } | |
284 | ||
285 | if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) { | |
1d14ffa9 FB |
286 | oss_logerr2 (errno, typ, "Failed to set number of channels %d\n", |
287 | req->nchannels); | |
85571bc7 FB |
288 | goto err; |
289 | } | |
290 | ||
291 | if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) { | |
1d14ffa9 | 292 | oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq); |
85571bc7 FB |
293 | goto err; |
294 | } | |
295 | ||
902e2b51 | 296 | if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) { |
1d14ffa9 | 297 | oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n"); |
85571bc7 FB |
298 | goto err; |
299 | } | |
300 | ||
78d9356d | 301 | #ifdef USE_DSP_POLICY |
baf6c7f4 | 302 | if (policy >= 0) { |
6d246526 | 303 | int version; |
0b3652bc | 304 | |
72ff25e4 | 305 | if (!oss_get_version (fd, &version, typ)) { |
d95d7d80 | 306 | trace_oss_version(version); |
6d246526 | 307 | |
3d709fe7 | 308 | if (version >= 0x040000) { |
baf6c7f4 KZ |
309 | int policy2 = policy; |
310 | if (ioctl(fd, SNDCTL_DSP_POLICY, &policy2)) { | |
3d709fe7 | 311 | oss_logerr2 (errno, typ, |
312 | "Failed to set timing policy to %d\n", | |
baf6c7f4 | 313 | policy); |
3d709fe7 | 314 | goto err; |
315 | } | |
316 | setfragment = 0; | |
6d246526 | 317 | } |
0b3652bc | 318 | } |
319 | } | |
0b3652bc | 320 | #endif |
3d709fe7 | 321 | |
322 | if (setfragment) { | |
0b3652bc | 323 | int mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize); |
324 | if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) { | |
325 | oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n", | |
326 | req->nfrags, req->fragsize); | |
327 | goto err; | |
328 | } | |
85571bc7 FB |
329 | } |
330 | ||
1d14ffa9 FB |
331 | if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) { |
332 | oss_logerr2 (errno, typ, "Failed to get buffer length\n"); | |
85571bc7 FB |
333 | goto err; |
334 | } | |
335 | ||
29ddf27b | 336 | if (!abinfo.fragstotal || !abinfo.fragsize) { |
337 | AUD_log (AUDIO_CAP, "Returned bogus buffer information(%d, %d) for %s\n", | |
338 | abinfo.fragstotal, abinfo.fragsize, typ); | |
339 | goto err; | |
340 | } | |
341 | ||
85571bc7 FB |
342 | obt->fmt = fmt; |
343 | obt->nchannels = nchannels; | |
344 | obt->freq = freq; | |
345 | obt->nfrags = abinfo.fragstotal; | |
346 | obt->fragsize = abinfo.fragsize; | |
347 | *pfd = fd; | |
348 | ||
c0fe3827 | 349 | #ifdef DEBUG_MISMATCHES |
85571bc7 FB |
350 | if ((req->fmt != obt->fmt) || |
351 | (req->nchannels != obt->nchannels) || | |
352 | (req->freq != obt->freq) || | |
353 | (req->fragsize != obt->fragsize) || | |
354 | (req->nfrags != obt->nfrags)) { | |
85571bc7 | 355 | dolog ("Audio parameters mismatch\n"); |
1d14ffa9 | 356 | oss_dump_info (req, obt); |
85571bc7 | 357 | } |
c0fe3827 | 358 | #endif |
85571bc7 | 359 | |
1d14ffa9 FB |
360 | #ifdef DEBUG |
361 | oss_dump_info (req, obt); | |
85571bc7 FB |
362 | #endif |
363 | return 0; | |
364 | ||
1d14ffa9 FB |
365 | err: |
366 | oss_anal_close (&fd); | |
85571bc7 FB |
367 | return -1; |
368 | } | |
369 | ||
3ba4066d | 370 | static size_t oss_get_available_bytes(OSSVoiceOut *oss) |
9d168976 | 371 | { |
3ba4066d KZ |
372 | int err; |
373 | struct count_info cntinfo; | |
374 | assert(oss->mmapped); | |
9d168976 | 375 | |
3ba4066d KZ |
376 | err = ioctl(oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); |
377 | if (err < 0) { | |
378 | oss_logerr(errno, "SNDCTL_DSP_GETOPTR failed\n"); | |
379 | return 0; | |
9d168976 | 380 | } |
381 | ||
3ba4066d KZ |
382 | return audio_ring_dist(cntinfo.ptr, oss->hw.pos_emul, oss->hw.size_emul); |
383 | } | |
9d168976 | 384 | |
fdc8c5f4 VR |
385 | static void oss_run_buffer_out(HWVoiceOut *hw) |
386 | { | |
387 | OSSVoiceOut *oss = (OSSVoiceOut *)hw; | |
388 | ||
389 | if (!oss->mmapped) { | |
390 | audio_generic_run_buffer_out(hw); | |
391 | } | |
392 | } | |
393 | ||
3ba4066d KZ |
394 | static void *oss_get_buffer_out(HWVoiceOut *hw, size_t *size) |
395 | { | |
396 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
397 | if (oss->mmapped) { | |
398 | *size = MIN(oss_get_available_bytes(oss), hw->size_emul - hw->pos_emul); | |
399 | return hw->buf_emul + hw->pos_emul; | |
400 | } else { | |
401 | return audio_generic_get_buffer_out(hw, size); | |
402 | } | |
403 | } | |
9d168976 | 404 | |
3ba4066d KZ |
405 | static size_t oss_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size) |
406 | { | |
407 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
408 | if (oss->mmapped) { | |
409 | assert(buf == hw->buf_emul + hw->pos_emul && size < hw->size_emul); | |
9d168976 | 410 | |
3ba4066d KZ |
411 | hw->pos_emul = (hw->pos_emul + size) % hw->size_emul; |
412 | return size; | |
413 | } else { | |
414 | return audio_generic_put_buffer_out(hw, buf, size); | |
9d168976 | 415 | } |
416 | } | |
417 | ||
3ba4066d | 418 | static size_t oss_write(HWVoiceOut *hw, void *buf, size_t len) |
85571bc7 | 419 | { |
1d14ffa9 | 420 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
3ba4066d | 421 | size_t pos; |
c0fe3827 | 422 | |
85571bc7 | 423 | if (oss->mmapped) { |
3ba4066d KZ |
424 | size_t total_len; |
425 | len = MIN(len, oss_get_available_bytes(oss)); | |
85571bc7 | 426 | |
3ba4066d KZ |
427 | total_len = len; |
428 | while (len) { | |
429 | size_t to_copy = MIN(len, hw->size_emul - hw->pos_emul); | |
430 | memcpy(hw->buf_emul + hw->pos_emul, buf, to_copy); | |
85571bc7 | 431 | |
7a4ede00 | 432 | hw->pos_emul = (hw->pos_emul + to_copy) % hw->size_emul; |
3ba4066d KZ |
433 | buf += to_copy; |
434 | len -= to_copy; | |
1d14ffa9 | 435 | } |
3ba4066d KZ |
436 | return total_len; |
437 | } | |
1d14ffa9 | 438 | |
3ba4066d KZ |
439 | pos = 0; |
440 | while (len) { | |
441 | ssize_t bytes_written; | |
442 | void *pcm = advance(buf, pos); | |
8ead62cf | 443 | |
3ba4066d KZ |
444 | bytes_written = write(oss->fd, pcm, len); |
445 | if (bytes_written < 0) { | |
446 | if (errno != EAGAIN) { | |
447 | oss_logerr(errno, "failed to write %zu bytes\n", | |
448 | len); | |
449 | } | |
450 | return pos; | |
85571bc7 FB |
451 | } |
452 | ||
3ba4066d KZ |
453 | pos += bytes_written; |
454 | if (bytes_written < len) { | |
455 | break; | |
1d14ffa9 | 456 | } |
3ba4066d | 457 | len -= bytes_written; |
85571bc7 | 458 | } |
3ba4066d | 459 | return pos; |
85571bc7 FB |
460 | } |
461 | ||
1d14ffa9 | 462 | static void oss_fini_out (HWVoiceOut *hw) |
85571bc7 FB |
463 | { |
464 | int err; | |
1d14ffa9 | 465 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 466 | |
1d14ffa9 FB |
467 | ldebug ("oss_fini\n"); |
468 | oss_anal_close (&oss->fd); | |
85571bc7 | 469 | |
3ba4066d KZ |
470 | if (oss->mmapped && hw->buf_emul) { |
471 | err = munmap(hw->buf_emul, hw->size_emul); | |
472 | if (err) { | |
473 | oss_logerr(errno, "Failed to unmap buffer %p, size %zu\n", | |
474 | hw->buf_emul, hw->size_emul); | |
85571bc7 | 475 | } |
3ba4066d | 476 | hw->buf_emul = NULL; |
85571bc7 FB |
477 | } |
478 | } | |
479 | ||
5706db1d KZ |
480 | static int oss_init_out(HWVoiceOut *hw, struct audsettings *as, |
481 | void *drv_opaque) | |
85571bc7 | 482 | { |
1d14ffa9 | 483 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
85571bc7 | 484 | struct oss_params req, obt; |
1d14ffa9 FB |
485 | int endianness; |
486 | int err; | |
487 | int fd; | |
85bc5852 | 488 | AudioFormat effective_fmt; |
1ea879e5 | 489 | struct audsettings obt_as; |
baf6c7f4 KZ |
490 | Audiodev *dev = drv_opaque; |
491 | AudiodevOssOptions *oopts = &dev->u.oss; | |
85571bc7 | 492 | |
571ec3d6 FB |
493 | oss->fd = -1; |
494 | ||
b6c9c940 | 495 | req.fmt = aud_to_ossfmt (as->fmt, as->endianness); |
c0fe3827 FB |
496 | req.freq = as->freq; |
497 | req.nchannels = as->nchannels; | |
85571bc7 | 498 | |
baf6c7f4 | 499 | if (oss_open(0, &req, as, &obt, &fd, dev)) { |
85571bc7 | 500 | return -1; |
1d14ffa9 | 501 | } |
85571bc7 | 502 | |
1d14ffa9 FB |
503 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); |
504 | if (err) { | |
505 | oss_anal_close (&fd); | |
506 | return -1; | |
507 | } | |
85571bc7 | 508 | |
c0fe3827 FB |
509 | obt_as.freq = obt.freq; |
510 | obt_as.nchannels = obt.nchannels; | |
511 | obt_as.fmt = effective_fmt; | |
d929eba5 | 512 | obt_as.endianness = endianness; |
c0fe3827 | 513 | |
d929eba5 | 514 | audio_pcm_init_info (&hw->info, &obt_as); |
85571bc7 FB |
515 | oss->nfrags = obt.nfrags; |
516 | oss->fragsize = obt.fragsize; | |
c0fe3827 | 517 | |
2b9cce8c | 518 | if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) { |
c0fe3827 | 519 | dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n", |
2b9cce8c | 520 | obt.nfrags * obt.fragsize, hw->info.bytes_per_frame); |
c0fe3827 FB |
521 | } |
522 | ||
2b9cce8c | 523 | hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame; |
85571bc7 FB |
524 | |
525 | oss->mmapped = 0; | |
baf6c7f4 | 526 | if (oopts->has_try_mmap && oopts->try_mmap) { |
2b9cce8c | 527 | hw->size_emul = hw->samples * hw->info.bytes_per_frame; |
3ba4066d | 528 | hw->buf_emul = mmap( |
660f11be | 529 | NULL, |
3ba4066d | 530 | hw->size_emul, |
c0fe3827 FB |
531 | PROT_READ | PROT_WRITE, |
532 | MAP_SHARED, | |
533 | fd, | |
534 | 0 | |
535 | ); | |
3ba4066d | 536 | if (hw->buf_emul == MAP_FAILED) { |
7520462b | 537 | oss_logerr(errno, "Failed to map %zu bytes of DAC\n", |
3ba4066d KZ |
538 | hw->size_emul); |
539 | hw->buf_emul = NULL; | |
540 | } else { | |
85571bc7 FB |
541 | int err; |
542 | int trig = 0; | |
1d14ffa9 FB |
543 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
544 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); | |
85571bc7 | 545 | } |
44a095a7 FB |
546 | else { |
547 | trig = PCM_ENABLE_OUTPUT; | |
1d14ffa9 FB |
548 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { |
549 | oss_logerr ( | |
550 | errno, | |
551 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
552 | ); | |
44a095a7 FB |
553 | } |
554 | else { | |
555 | oss->mmapped = 1; | |
556 | } | |
85571bc7 | 557 | } |
85571bc7 | 558 | |
44a095a7 | 559 | if (!oss->mmapped) { |
3ba4066d | 560 | err = munmap(hw->buf_emul, hw->size_emul); |
44a095a7 | 561 | if (err) { |
7520462b | 562 | oss_logerr(errno, "Failed to unmap buffer %p size %zu\n", |
3ba4066d | 563 | hw->buf_emul, hw->size_emul); |
44a095a7 | 564 | } |
3ba4066d | 565 | hw->buf_emul = NULL; |
85571bc7 FB |
566 | } |
567 | } | |
568 | } | |
569 | ||
1d14ffa9 | 570 | oss->fd = fd; |
baf6c7f4 | 571 | oss->dev = dev; |
85571bc7 FB |
572 | return 0; |
573 | } | |
574 | ||
571a8c52 | 575 | static void oss_enable_out(HWVoiceOut *hw, bool enable) |
85571bc7 FB |
576 | { |
577 | int trig; | |
1d14ffa9 | 578 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; |
baf6c7f4 | 579 | AudiodevOssPerDirectionOptions *opdo = oss->dev->u.oss.out; |
85571bc7 | 580 | |
571a8c52 KZ |
581 | if (enable) { |
582 | bool poll_mode = opdo->try_poll; | |
dd8a5649 | 583 | |
571a8c52 KZ |
584 | ldebug("enabling voice\n"); |
585 | if (poll_mode) { | |
586 | oss_poll_out(hw); | |
587 | poll_mode = 0; | |
588 | } | |
589 | hw->poll_mode = poll_mode; | |
301901b5 | 590 | |
571a8c52 KZ |
591 | if (!oss->mmapped) { |
592 | return; | |
85571bc7 | 593 | } |
85571bc7 | 594 | |
571a8c52 KZ |
595 | audio_pcm_info_clear_buf(&hw->info, hw->buf_emul, hw->mix_buf->size); |
596 | trig = PCM_ENABLE_OUTPUT; | |
597 | if (ioctl(oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
598 | oss_logerr(errno, | |
599 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"); | |
600 | return; | |
601 | } | |
602 | } else { | |
dd8a5649 | 603 | if (hw->poll_mode) { |
604 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
605 | hw->poll_mode = 0; | |
606 | } | |
607 | ||
608 | if (!oss->mmapped) { | |
571a8c52 | 609 | return; |
dd8a5649 | 610 | } |
611 | ||
85571bc7 FB |
612 | ldebug ("disabling voice\n"); |
613 | trig = 0; | |
614 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
1d14ffa9 | 615 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); |
571a8c52 | 616 | return; |
85571bc7 | 617 | } |
85571bc7 | 618 | } |
85571bc7 FB |
619 | } |
620 | ||
5706db1d | 621 | static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) |
1d14ffa9 FB |
622 | { |
623 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
624 | struct oss_params req, obt; | |
625 | int endianness; | |
626 | int err; | |
627 | int fd; | |
85bc5852 | 628 | AudioFormat effective_fmt; |
1ea879e5 | 629 | struct audsettings obt_as; |
baf6c7f4 | 630 | Audiodev *dev = drv_opaque; |
1d14ffa9 | 631 | |
571ec3d6 FB |
632 | oss->fd = -1; |
633 | ||
b6c9c940 | 634 | req.fmt = aud_to_ossfmt (as->fmt, as->endianness); |
c0fe3827 FB |
635 | req.freq = as->freq; |
636 | req.nchannels = as->nchannels; | |
baf6c7f4 | 637 | if (oss_open(1, &req, as, &obt, &fd, dev)) { |
1d14ffa9 FB |
638 | return -1; |
639 | } | |
640 | ||
641 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); | |
642 | if (err) { | |
643 | oss_anal_close (&fd); | |
644 | return -1; | |
645 | } | |
646 | ||
c0fe3827 FB |
647 | obt_as.freq = obt.freq; |
648 | obt_as.nchannels = obt.nchannels; | |
649 | obt_as.fmt = effective_fmt; | |
d929eba5 | 650 | obt_as.endianness = endianness; |
c0fe3827 | 651 | |
d929eba5 | 652 | audio_pcm_init_info (&hw->info, &obt_as); |
1d14ffa9 FB |
653 | oss->nfrags = obt.nfrags; |
654 | oss->fragsize = obt.fragsize; | |
c0fe3827 | 655 | |
2b9cce8c | 656 | if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) { |
c0fe3827 | 657 | dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n", |
2b9cce8c | 658 | obt.nfrags * obt.fragsize, hw->info.bytes_per_frame); |
c0fe3827 FB |
659 | } |
660 | ||
2b9cce8c | 661 | hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame; |
1d14ffa9 FB |
662 | |
663 | oss->fd = fd; | |
baf6c7f4 | 664 | oss->dev = dev; |
1d14ffa9 FB |
665 | return 0; |
666 | } | |
667 | ||
668 | static void oss_fini_in (HWVoiceIn *hw) | |
669 | { | |
670 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
671 | ||
672 | oss_anal_close (&oss->fd); | |
1d14ffa9 FB |
673 | } |
674 | ||
3ba4066d | 675 | static size_t oss_read(HWVoiceIn *hw, void *buf, size_t len) |
1d14ffa9 FB |
676 | { |
677 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; | |
3ba4066d | 678 | size_t pos = 0; |
1d14ffa9 | 679 | |
3ba4066d | 680 | while (len) { |
1d14ffa9 FB |
681 | ssize_t nread; |
682 | ||
3ba4066d KZ |
683 | void *dst = advance(buf, pos); |
684 | nread = read(oss->fd, dst, len); | |
1d14ffa9 | 685 | |
3ba4066d KZ |
686 | if (nread == -1) { |
687 | switch (errno) { | |
688 | case EINTR: | |
689 | case EAGAIN: | |
690 | break; | |
691 | default: | |
692 | oss_logerr(errno, "Failed to read %zu bytes of audio (to %p)\n", | |
693 | len, dst); | |
1d14ffa9 FB |
694 | break; |
695 | } | |
696 | } | |
3ba4066d KZ |
697 | |
698 | pos += nread; | |
699 | len -= nread; | |
1d14ffa9 FB |
700 | } |
701 | ||
3ba4066d | 702 | return pos; |
1d14ffa9 FB |
703 | } |
704 | ||
571a8c52 | 705 | static void oss_enable_in(HWVoiceIn *hw, bool enable) |
1d14ffa9 | 706 | { |
dd8a5649 | 707 | OSSVoiceIn *oss = (OSSVoiceIn *) hw; |
baf6c7f4 | 708 | AudiodevOssPerDirectionOptions *opdo = oss->dev->u.oss.out; |
dd8a5649 | 709 | |
571a8c52 KZ |
710 | if (enable) { |
711 | bool poll_mode = opdo->try_poll; | |
a628b869 | 712 | |
571a8c52 KZ |
713 | if (poll_mode) { |
714 | oss_poll_in(hw); | |
715 | poll_mode = 0; | |
dd8a5649 | 716 | } |
571a8c52 KZ |
717 | hw->poll_mode = poll_mode; |
718 | } else { | |
dd8a5649 | 719 | if (hw->poll_mode) { |
720 | hw->poll_mode = 0; | |
721 | qemu_set_fd_handler (oss->fd, NULL, NULL, NULL); | |
722 | } | |
dd8a5649 | 723 | } |
1d14ffa9 FB |
724 | } |
725 | ||
baf6c7f4 KZ |
726 | static void oss_init_per_direction(AudiodevOssPerDirectionOptions *opdo) |
727 | { | |
728 | if (!opdo->has_try_poll) { | |
729 | opdo->try_poll = true; | |
730 | opdo->has_try_poll = true; | |
731 | } | |
732 | } | |
4045a85a | 733 | |
71830221 | 734 | static void *oss_audio_init(Audiodev *dev) |
85571bc7 | 735 | { |
baf6c7f4 KZ |
736 | AudiodevOssOptions *oopts; |
737 | assert(dev->driver == AUDIODEV_DRIVER_OSS); | |
738 | ||
739 | oopts = &dev->u.oss; | |
740 | oss_init_per_direction(oopts->in); | |
741 | oss_init_per_direction(oopts->out); | |
4045a85a | 742 | |
baf6c7f4 KZ |
743 | if (access(oopts->in->has_dev ? oopts->in->dev : "/dev/dsp", |
744 | R_OK | W_OK) < 0 || | |
745 | access(oopts->out->has_dev ? oopts->out->dev : "/dev/dsp", | |
746 | R_OK | W_OK) < 0) { | |
73204cff GH |
747 | return NULL; |
748 | } | |
baf6c7f4 | 749 | return dev; |
85571bc7 FB |
750 | } |
751 | ||
752 | static void oss_audio_fini (void *opaque) | |
753 | { | |
754 | } | |
755 | ||
35f4b58c | 756 | static struct audio_pcm_ops oss_pcm_ops = { |
1dd3e4d1 JQ |
757 | .init_out = oss_init_out, |
758 | .fini_out = oss_fini_out, | |
3ba4066d | 759 | .write = oss_write, |
fdc8c5f4 | 760 | .run_buffer_out = oss_run_buffer_out, |
3ba4066d KZ |
761 | .get_buffer_out = oss_get_buffer_out, |
762 | .put_buffer_out = oss_put_buffer_out, | |
571a8c52 | 763 | .enable_out = oss_enable_out, |
1dd3e4d1 JQ |
764 | |
765 | .init_in = oss_init_in, | |
766 | .fini_in = oss_fini_in, | |
3ba4066d | 767 | .read = oss_read, |
571a8c52 | 768 | .enable_in = oss_enable_in |
85571bc7 FB |
769 | }; |
770 | ||
d3893a39 | 771 | static struct audio_driver oss_audio_driver = { |
bee37f32 JQ |
772 | .name = "oss", |
773 | .descr = "OSS http://www.opensound.com", | |
bee37f32 JQ |
774 | .init = oss_audio_init, |
775 | .fini = oss_audio_fini, | |
776 | .pcm_ops = &oss_pcm_ops, | |
926de754 | 777 | .can_be_default = 1, |
bee37f32 JQ |
778 | .max_voices_out = INT_MAX, |
779 | .max_voices_in = INT_MAX, | |
780 | .voice_size_out = sizeof (OSSVoiceOut), | |
781 | .voice_size_in = sizeof (OSSVoiceIn) | |
85571bc7 | 782 | }; |
d3893a39 GH |
783 | |
784 | static void register_audio_oss(void) | |
785 | { | |
786 | audio_driver_register(&oss_audio_driver); | |
787 | } | |
788 | type_init(register_audio_oss); |