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