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