Work around OpenSolaris sys/regset.h namespace pollution
[qemu] / audio / ossaudio.c
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 #ifdef __OpenBSD__
29 #include <soundcard.h>
30 #else
31 #include <sys/soundcard.h>
32 #endif
33 #include "qemu-common.h"
34 #include "host-utils.h"
35 #include "qemu-char.h"
36 #include "audio.h"
37
38 #define AUDIO_CAP "oss"
39 #include "audio_int.h"
40
41 typedef struct OSSVoiceOut {
42     HWVoiceOut hw;
43     void *pcm_buf;
44     int fd;
45     int nfrags;
46     int fragsize;
47     int mmapped;
48     int old_optr;
49 } OSSVoiceOut;
50
51 typedef struct OSSVoiceIn {
52     HWVoiceIn hw;
53     void *pcm_buf;
54     int fd;
55     int nfrags;
56     int fragsize;
57     int old_optr;
58 } OSSVoiceIn;
59
60 static struct {
61     int try_mmap;
62     int nfrags;
63     int fragsize;
64     const char *devpath_out;
65     const char *devpath_in;
66     int debug;
67 } conf = {
68     .try_mmap = 0,
69     .nfrags = 4,
70     .fragsize = 4096,
71     .devpath_out = "/dev/dsp",
72     .devpath_in = "/dev/dsp",
73     .debug = 0
74 };
75
76 struct oss_params {
77     int freq;
78     audfmt_e fmt;
79     int nchannels;
80     int nfrags;
81     int fragsize;
82 };
83
84 static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...)
85 {
86     va_list ap;
87
88     va_start (ap, fmt);
89     AUD_vlog (AUDIO_CAP, fmt, ap);
90     va_end (ap);
91
92     AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
93 }
94
95 static 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
104     AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
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
113 static 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     }
119     qemu_set_fd_handler (*fdp, NULL, NULL, NULL);
120     *fdp = -1;
121 }
122
123 static void oss_helper_poll_out (void *opaque)
124 {
125     (void) opaque;
126     audio_run ("oss_poll_out");
127 }
128
129 static void oss_helper_poll_in (void *opaque)
130 {
131     (void) opaque;
132     audio_run ("oss_poll_in");
133 }
134
135 static 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
142 static 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
149 static int oss_write (SWVoiceOut *sw, void *buf, int len)
150 {
151     return audio_pcm_sw_write (sw, buf, len);
152 }
153
154 static int aud_to_ossfmt (audfmt_e fmt)
155 {
156     switch (fmt) {
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
169     default:
170         dolog ("Internal logic error: Bad audio format %d\n", fmt);
171 #ifdef DEBUG_AUDIO
172         abort ();
173 #endif
174         return AFMT_U8;
175     }
176 }
177
178 static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness)
179 {
180     switch (ossfmt) {
181     case AFMT_S8:
182         *endianness = 0;
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
211     default:
212         dolog ("Unrecognized audio format %d\n", ossfmt);
213         return -1;
214     }
215
216     return 0;
217 }
218
219 #if defined DEBUG_MISMATCHES || defined DEBUG
220 static void oss_dump_info (struct oss_params *req, struct oss_params *obt)
221 {
222     dolog ("parameter | requested value | obtained value\n");
223     dolog ("format    |      %10d |     %10d\n", req->fmt, obt->fmt);
224     dolog ("channels  |      %10d |     %10d\n",
225            req->nchannels, obt->nchannels);
226     dolog ("frequency |      %10d |     %10d\n", req->freq, obt->freq);
227     dolog ("nfrags    |      %10d |     %10d\n", req->nfrags, obt->nfrags);
228     dolog ("fragsize  |      %10d |     %10d\n",
229            req->fragsize, obt->fragsize);
230 }
231 #endif
232
233 static int oss_open (int in, struct oss_params *req,
234                      struct oss_params *obt, int *pfd)
235 {
236     int fd;
237     int oflags;
238     int mmmmssss;
239     audio_buf_info abinfo;
240     int fmt, freq, nchannels;
241     const char *dspname = in ? conf.devpath_in : conf.devpath_out;
242     const char *typ = in ? "ADC" : "DAC";
243
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);
247     if (-1 == fd) {
248         oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname);
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)) {
257         oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt);
258         goto err;
259     }
260
261     if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
262         oss_logerr2 (errno, typ, "Failed to set number of channels %d\n",
263                      req->nchannels);
264         goto err;
265     }
266
267     if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
268         oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq);
269         goto err;
270     }
271
272     if (ioctl (fd, SNDCTL_DSP_NONBLOCK, NULL)) {
273         oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n");
274         goto err;
275     }
276
277     mmmmssss = (req->nfrags << 16) | ctz32 (req->fragsize);
278     if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
279         oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n",
280                      req->nfrags, req->fragsize);
281         goto err;
282     }
283
284     if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) {
285         oss_logerr2 (errno, typ, "Failed to get buffer length\n");
286         goto err;
287     }
288
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
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
302 #ifdef DEBUG_MISMATCHES
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)) {
308         dolog ("Audio parameters mismatch\n");
309         oss_dump_info (req, obt);
310     }
311 #endif
312
313 #ifdef DEBUG
314     oss_dump_info (req, obt);
315 #endif
316     return 0;
317
318  err:
319     oss_anal_close (&fd);
320     return -1;
321 }
322
323 static int oss_run_out (HWVoiceOut *hw)
324 {
325     OSSVoiceOut *oss = (OSSVoiceOut *) hw;
326     int err, rpos, live, decr;
327     int samples;
328     uint8_t *dst;
329     struct st_sample *src;
330     struct audio_buf_info abinfo;
331     struct count_info cntinfo;
332     int bufsize;
333
334     live = audio_pcm_hw_get_live_out (hw);
335     if (!live) {
336         return 0;
337     }
338
339     bufsize = hw->samples << hw->info.shift;
340
341     if (oss->mmapped) {
342         int bytes;
343
344         err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
345         if (err < 0) {
346             oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
347             return 0;
348         }
349
350         if (cntinfo.ptr == oss->old_optr) {
351             if (abs (hw->samples - live) < 64) {
352                 dolog ("warning: Overrun\n");
353             }
354             return 0;
355         }
356
357         if (cntinfo.ptr > oss->old_optr) {
358             bytes = cntinfo.ptr - oss->old_optr;
359         }
360         else {
361             bytes = bufsize + cntinfo.ptr - oss->old_optr;
362         }
363
364         decr = audio_MIN (bytes >> hw->info.shift, live);
365     }
366     else {
367         err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
368         if (err < 0) {
369             oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n");
370             return 0;
371         }
372
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             }
387             return 0;
388         }
389
390         decr = audio_MIN (abinfo.bytes >> hw->info.shift, live);
391         if (!decr) {
392             return 0;
393         }
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
402         src = hw->mix_buf + rpos;
403         dst = advance (oss->pcm_buf, rpos << hw->info.shift);
404
405         hw->clip (dst, src, convert_samples);
406         if (!oss->mmapped) {
407             int written;
408
409             written = write (oss->fd, dst, convert_samples << hw->info.shift);
410             /* XXX: follow errno recommendations ? */
411             if (written == -1) {
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                     );
418                 continue;
419             }
420
421             if (written != convert_samples << hw->info.shift) {
422                 int wsamples = written >> hw->info.shift;
423                 int wbytes = wsamples << hw->info.shift;
424                 if (wbytes != written) {
425                     dolog ("warning: Misaligned write %d (requested %d), "
426                            "alignment %d\n",
427                            wbytes, written, hw->info.align + 1);
428                 }
429                 decr -= wsamples;
430                 rpos = (rpos + wsamples) % hw->samples;
431                 break;
432             }
433         }
434
435         rpos = (rpos + convert_samples) % hw->samples;
436         samples -= convert_samples;
437     }
438     if (oss->mmapped) {
439         oss->old_optr = cntinfo.ptr;
440     }
441
442     hw->rpos = rpos;
443     return decr;
444 }
445
446 static void oss_fini_out (HWVoiceOut *hw)
447 {
448     int err;
449     OSSVoiceOut *oss = (OSSVoiceOut *) hw;
450
451     ldebug ("oss_fini\n");
452     oss_anal_close (&oss->fd);
453
454     if (oss->pcm_buf) {
455         if (oss->mmapped) {
456             err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
457             if (err) {
458                 oss_logerr (errno, "Failed to unmap buffer %p, size %d\n",
459                             oss->pcm_buf, hw->samples << hw->info.shift);
460             }
461         }
462         else {
463             qemu_free (oss->pcm_buf);
464         }
465         oss->pcm_buf = NULL;
466     }
467 }
468
469 static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
470 {
471     OSSVoiceOut *oss = (OSSVoiceOut *) hw;
472     struct oss_params req, obt;
473     int endianness;
474     int err;
475     int fd;
476     audfmt_e effective_fmt;
477     struct audsettings obt_as;
478
479     oss->fd = -1;
480
481     req.fmt = aud_to_ossfmt (as->fmt);
482     req.freq = as->freq;
483     req.nchannels = as->nchannels;
484     req.fragsize = conf.fragsize;
485     req.nfrags = conf.nfrags;
486
487     if (oss_open (0, &req, &obt, &fd)) {
488         return -1;
489     }
490
491     err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness);
492     if (err) {
493         oss_anal_close (&fd);
494         return -1;
495     }
496
497     obt_as.freq = obt.freq;
498     obt_as.nchannels = obt.nchannels;
499     obt_as.fmt = effective_fmt;
500     obt_as.endianness = endianness;
501
502     audio_pcm_init_info (&hw->info, &obt_as);
503     oss->nfrags = obt.nfrags;
504     oss->fragsize = obt.fragsize;
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;
512
513     oss->mmapped = 0;
514     if (conf.try_mmap) {
515         oss->pcm_buf = mmap (
516             NULL,
517             hw->samples << hw->info.shift,
518             PROT_READ | PROT_WRITE,
519             MAP_SHARED,
520             fd,
521             0
522             );
523         if (oss->pcm_buf == MAP_FAILED) {
524             oss_logerr (errno, "Failed to map %d bytes of DAC\n",
525                         hw->samples << hw->info.shift);
526         } else {
527             int err;
528             int trig = 0;
529             if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
530                 oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
531             }
532             else {
533                 trig = PCM_ENABLE_OUTPUT;
534                 if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
535                     oss_logerr (
536                         errno,
537                         "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
538                         );
539                 }
540                 else {
541                     oss->mmapped = 1;
542                 }
543             }
544
545             if (!oss->mmapped) {
546                 err = munmap (oss->pcm_buf, hw->samples << hw->info.shift);
547                 if (err) {
548                     oss_logerr (errno, "Failed to unmap buffer %p size %d\n",
549                                 oss->pcm_buf, hw->samples << hw->info.shift);
550                 }
551             }
552         }
553     }
554
555     if (!oss->mmapped) {
556         oss->pcm_buf = audio_calloc (
557             AUDIO_FUNC,
558             hw->samples,
559             1 << hw->info.shift
560             );
561         if (!oss->pcm_buf) {
562             dolog (
563                 "Could not allocate DAC buffer (%d samples, each %d bytes)\n",
564                 hw->samples,
565                 1 << hw->info.shift
566                 );
567             oss_anal_close (&fd);
568             return -1;
569         }
570     }
571
572     oss->fd = fd;
573     return 0;
574 }
575
576 static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
577 {
578     int trig;
579     va_list ap;
580     int poll_mode;
581     OSSVoiceOut *oss = (OSSVoiceOut *) hw;
582
583     va_start (ap, cmd);
584     poll_mode = va_arg (ap, int);
585     va_end (ap);
586
587     switch (cmd) {
588     case VOICE_ENABLE:
589         ldebug ("enabling voice\n");
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
599         audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples);
600         trig = PCM_ENABLE_OUTPUT;
601         if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
602             oss_logerr (
603                 errno,
604                 "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
605                 );
606             return -1;
607         }
608         break;
609
610     case VOICE_DISABLE:
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
620         ldebug ("disabling voice\n");
621         trig = 0;
622         if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
623             oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n");
624             return -1;
625         }
626         break;
627     }
628     return 0;
629 }
630
631 static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
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;
639     struct audsettings obt_as;
640
641     oss->fd = -1;
642
643     req.fmt = aud_to_ossfmt (as->fmt);
644     req.freq = as->freq;
645     req.nchannels = as->nchannels;
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
658     obt_as.freq = obt.freq;
659     obt_as.nchannels = obt.nchannels;
660     obt_as.fmt = effective_fmt;
661     obt_as.endianness = endianness;
662
663     audio_pcm_init_info (&hw->info, &obt_as);
664     oss->nfrags = obt.nfrags;
665     oss->fragsize = obt.fragsize;
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);
674     if (!oss->pcm_buf) {
675         dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n",
676                hw->samples, 1 << hw->info.shift);
677         oss_anal_close (&fd);
678         return -1;
679     }
680
681     oss->fd = fd;
682     return 0;
683 }
684
685 static 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
697 static 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] = {
709         { .add = hw->wpos, .len = 0 },
710         { .add = 0,        .len = 0 }
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) {
735                     dolog ("warning: Misaligned read %zd (requested %d), "
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
768 static int oss_read (SWVoiceIn *sw, void *buf, int size)
769 {
770     return audio_pcm_sw_read (sw, buf, size);
771 }
772
773 static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
774 {
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     }
798     return 0;
799 }
800
801 static void *oss_audio_init (void)
802 {
803     return &conf;
804 }
805
806 static void oss_audio_fini (void *opaque)
807 {
808     (void) opaque;
809 }
810
811 static struct audio_option oss_options[] = {
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     },
848     { /* End of list */ }
849 };
850
851 static struct audio_pcm_ops oss_pcm_ops = {
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
863 };
864
865 struct audio_driver oss_audio_driver = {
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)
877 };