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