2 * QEMU OSS audio output driver
4 * Copyright (c) 2003-2004 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/soundcard.h>
31 #include "audio/audio_int.h"
33 typedef struct OSSVoice {
44 #define dolog(...) AUD_log ("oss", __VA_ARGS__)
46 #define ldebug(...) dolog (__VA_ARGS__)
51 #define QC_OSS_FRAGSIZE "QEMU_OSS_FRAGSIZE"
52 #define QC_OSS_NFRAGS "QEMU_OSS_NFRAGS"
53 #define QC_OSS_MMAP "QEMU_OSS_MMAP"
54 #define QC_OSS_DEV "QEMU_OSS_DEV"
56 #define errstr() strerror (errno)
78 static int oss_hw_write (SWVoice *sw, void *buf, int len)
80 return pcm_hw_write (sw, buf, len);
83 static int AUD_to_ossfmt (audfmt_e fmt)
86 case AUD_FMT_S8: return AFMT_S8;
87 case AUD_FMT_U8: return AFMT_U8;
88 case AUD_FMT_S16: return AFMT_S16_LE;
89 case AUD_FMT_U16: return AFMT_U16_LE;
91 dolog ("Internal logic error: Bad audio format %d\nAborting\n", fmt);
96 static int oss_to_audfmt (int fmt)
99 case AFMT_S8: return AUD_FMT_S8;
100 case AFMT_U8: return AUD_FMT_U8;
101 case AFMT_S16_LE: return AUD_FMT_S16;
102 case AFMT_U16_LE: return AUD_FMT_U16;
104 dolog ("Internal logic error: Unrecognized OSS audio format %d\n"
112 static void oss_dump_pcm_info (struct oss_params *req, struct oss_params *obt)
114 dolog ("parameter | requested value | obtained value\n");
115 dolog ("format | %10d | %10d\n", req->fmt, obt->fmt);
116 dolog ("channels | %10d | %10d\n", req->nchannels, obt->nchannels);
117 dolog ("frequency | %10d | %10d\n", req->freq, obt->freq);
118 dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags);
119 dolog ("fragsize | %10d | %10d\n", req->fragsize, obt->fragsize);
123 static int oss_open (struct oss_params *req, struct oss_params *obt, int *pfd)
127 audio_buf_info abinfo;
128 int fmt, freq, nchannels;
129 const char *dspname = conf.dspname;
131 fd = open (dspname, O_RDWR | O_NONBLOCK);
133 dolog ("Could not initialize audio hardware. Failed to open `%s':\n"
141 nchannels = req->nchannels;
144 if (ioctl (fd, SNDCTL_DSP_SAMPLESIZE, &fmt)) {
145 dolog ("Could not initialize audio hardware\n"
146 "Failed to set sample size\n"
152 if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) {
153 dolog ("Could not initialize audio hardware\n"
154 "Failed to set number of channels\n"
160 if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) {
161 dolog ("Could not initialize audio hardware\n"
162 "Failed to set frequency\n"
168 if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) {
169 dolog ("Could not initialize audio hardware\n"
170 "Failed to set non-blocking mode\n"
176 mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize);
177 if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) {
178 dolog ("Could not initialize audio hardware\n"
179 "Failed to set buffer length (%d, %d)\n"
181 conf.nfrags, conf.fragsize,
186 if (ioctl (fd, SNDCTL_DSP_GETOSPACE, &abinfo)) {
187 dolog ("Could not initialize audio hardware\n"
188 "Failed to get buffer length\n"
195 obt->nchannels = nchannels;
197 obt->nfrags = abinfo.fragstotal;
198 obt->fragsize = abinfo.fragsize;
201 if ((req->fmt != obt->fmt) ||
202 (req->nchannels != obt->nchannels) ||
203 (req->freq != obt->freq) ||
204 (req->fragsize != obt->fragsize) ||
205 (req->nfrags != obt->nfrags)) {
207 dolog ("Audio parameters mismatch\n");
208 oss_dump_pcm_info (req, obt);
213 oss_dump_pcm_info (req, obt);
222 static void oss_hw_run (HWVoice *hw)
224 OSSVoice *oss = (OSSVoice *) hw;
225 int err, rpos, live, decr;
229 struct audio_buf_info abinfo;
230 struct count_info cntinfo;
232 live = pcm_hw_get_live (hw);
239 err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo);
241 dolog ("SNDCTL_DSP_GETOPTR failed\nReason: %s\n", errstr ());
245 if (cntinfo.ptr == oss->old_optr) {
246 if (abs (hw->samples - live) < 64)
251 if (cntinfo.ptr > oss->old_optr) {
252 bytes = cntinfo.ptr - oss->old_optr;
255 bytes = hw->bufsize + cntinfo.ptr - oss->old_optr;
258 decr = audio_MIN (bytes >> hw->shift, live);
261 err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo);
263 dolog ("SNDCTL_DSP_GETOSPACE failed\nReason: %s\n", errstr ());
267 decr = audio_MIN (abinfo.bytes >> hw->shift, live);
275 int left_till_end_samples = hw->samples - rpos;
276 int convert_samples = audio_MIN (samples, left_till_end_samples);
278 src = advance (hw->mix_buf, rpos * sizeof (st_sample_t));
279 dst = advance (oss->pcm_buf, rpos << hw->shift);
281 hw->clip (dst, src, convert_samples);
285 written = write (oss->fd, dst, convert_samples << hw->shift);
286 /* XXX: follow errno recommendations ? */
288 dolog ("Failed to write audio\nReason: %s\n", errstr ());
292 if (written != convert_samples << hw->shift) {
293 int wsamples = written >> hw->shift;
294 int wbytes = wsamples << hw->shift;
295 if (wbytes != written) {
296 dolog ("Unaligned write %d, %d\n", wbytes, written);
298 memset (src, 0, wbytes);
300 rpos = (rpos + wsamples) % hw->samples;
304 memset (src, 0, convert_samples * sizeof (st_sample_t));
306 rpos = (rpos + convert_samples) % hw->samples;
307 samples -= convert_samples;
310 oss->old_optr = cntinfo.ptr;
313 pcm_hw_dec_live (hw, decr);
317 static void oss_hw_fini (HWVoice *hw)
320 OSSVoice *oss = (OSSVoice *) hw;
322 ldebug ("oss_hw_fini\n");
323 err = close (oss->fd);
325 dolog ("Failed to close OSS descriptor\nReason: %s\n", errstr ());
331 err = munmap (oss->pcm_buf, hw->bufsize);
333 dolog ("Failed to unmap OSS buffer\nReason: %s\n",
338 qemu_free (oss->pcm_buf);
344 static int oss_hw_init (HWVoice *hw, int freq, int nchannels, audfmt_e fmt)
346 OSSVoice *oss = (OSSVoice *) hw;
347 struct oss_params req, obt;
350 req.fmt = AUD_to_ossfmt (fmt);
352 req.nchannels = nchannels;
353 req.fragsize = conf.fragsize;
354 req.nfrags = conf.nfrags;
356 if (oss_open (&req, &obt, &oss->fd))
360 hw->fmt = oss_to_audfmt (obt.fmt);
361 hw->nchannels = obt.nchannels;
363 oss->nfrags = obt.nfrags;
364 oss->fragsize = obt.fragsize;
365 hw->bufsize = obt.nfrags * obt.fragsize;
369 oss->pcm_buf = mmap (0, hw->bufsize, PROT_READ | PROT_WRITE,
370 MAP_SHARED, oss->fd, 0);
371 if (oss->pcm_buf == MAP_FAILED) {
372 dolog ("Failed to mmap OSS device\nReason: %s\n",
379 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
380 dolog ("SNDCTL_DSP_SETTRIGGER 0 failed\nReason: %s\n",
385 trig = PCM_ENABLE_OUTPUT;
386 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
387 dolog ("SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
388 "Reason: %s\n", errstr ());
395 err = munmap (oss->pcm_buf, hw->bufsize);
397 dolog ("Failed to unmap OSS device\nReason: %s\n",
404 oss->pcm_buf = qemu_mallocz (hw->bufsize);
415 static int oss_hw_ctl (HWVoice *hw, int cmd, ...)
418 OSSVoice *oss = (OSSVoice *) hw;
425 ldebug ("enabling voice\n");
426 pcm_hw_clear (hw, oss->pcm_buf, hw->samples);
427 trig = PCM_ENABLE_OUTPUT;
428 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
429 dolog ("SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n"
430 "Reason: %s\n", errstr ());
436 ldebug ("disabling voice\n");
438 if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) {
439 dolog ("SNDCTL_DSP_SETTRIGGER 0 failed\nReason: %s\n",
448 static void *oss_audio_init (void)
450 conf.fragsize = audio_get_conf_int (QC_OSS_FRAGSIZE, conf.fragsize);
451 conf.nfrags = audio_get_conf_int (QC_OSS_NFRAGS, conf.nfrags);
452 conf.try_mmap = audio_get_conf_int (QC_OSS_MMAP, conf.try_mmap);
453 conf.dspname = audio_get_conf_str (QC_OSS_DEV, conf.dspname);
457 static void oss_audio_fini (void *opaque)
461 struct pcm_ops oss_pcm_ops = {
469 struct audio_output_driver oss_output_driver = {