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