Statistics
| Branch: | Revision:

root / audio / ossaudio.c @ 077805fa

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