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