Statistics
| Branch: | Revision:

root / audio / ossaudio.c @ 06ea77bc

History | View | Annotate | Download (23.9 kB)

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