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