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