]>
Commit | Line | Data |
---|---|---|
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 <sys/mman.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/ioctl.h> | |
27 | #include <sys/soundcard.h> | |
28 | #include "vl.h" | |
29 | ||
30 | #define AUDIO_CAP "oss" | |
31 | #include "audio_int.h" | |
32 | ||
33 | typedef struct OSSVoiceOut { | |
34 | HWVoiceOut hw; | |
35 | void *pcm_buf; | |
36 | int fd; | |
37 | int nfrags; | |
38 | int fragsize; | |
39 | int mmapped; | |
40 | int old_optr; | |
41 | } OSSVoiceOut; | |
42 | ||
43 | typedef struct OSSVoiceIn { | |
44 | HWVoiceIn hw; | |
45 | void *pcm_buf; | |
46 | int fd; | |
47 | int nfrags; | |
48 | int fragsize; | |
49 | int old_optr; | |
50 | } OSSVoiceIn; | |
51 | ||
52 | static struct { | |
53 | int try_mmap; | |
54 | int nfrags; | |
55 | int fragsize; | |
56 | const char *devpath_out; | |
57 | const char *devpath_in; | |
58 | int debug; | |
59 | } conf = { | |
60 | .try_mmap = 0, | |
61 | .nfrags = 4, | |
62 | .fragsize = 4096, | |
63 | .devpath_out = "/dev/dsp", | |
64 | .devpath_in = "/dev/dsp", | |
65 | .debug = 0 | |
66 | }; | |
67 | ||
68 | struct oss_params { | |
69 | int freq; | |
70 | audfmt_e fmt; | |
71 | int nchannels; | |
72 | int nfrags; | |
73 | int fragsize; | |
74 | }; | |
75 | ||
76 | static void GCC_FMT_ATTR (2, 3) oss_logerr (int err, const char *fmt, ...) | |
77 | { | |
78 | va_list ap; | |
79 | ||
80 | va_start (ap, fmt); | |
81 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
82 | va_end (ap); | |
83 | ||
84 | AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err)); | |
85 | } | |
86 | ||
87 | static 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 | ||
96 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); | |
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 | ||
105 | static 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 | ||
114 | static int oss_write (SWVoiceOut *sw, void *buf, int len) | |
115 | { | |
116 | return audio_pcm_sw_write (sw, buf, len); | |
117 | } | |
118 | ||
119 | static int aud_to_ossfmt (audfmt_e fmt) | |
120 | { | |
121 | switch (fmt) { | |
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 | ||
134 | default: | |
135 | dolog ("Internal logic error: Bad audio format %d\n", fmt); | |
136 | #ifdef DEBUG_AUDIO | |
137 | abort (); | |
138 | #endif | |
139 | return AFMT_U8; | |
140 | } | |
141 | } | |
142 | ||
143 | static int oss_to_audfmt (int ossfmt, audfmt_e *fmt, int *endianness) | |
144 | { | |
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 | ||
176 | default: | |
177 | dolog ("Unrecognized audio format %d\n", ossfmt); | |
178 | return -1; | |
179 | } | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
184 | #if defined DEBUG_MISMATCHES || defined DEBUG | |
185 | static void oss_dump_info (struct oss_params *req, struct oss_params *obt) | |
186 | { | |
187 | dolog ("parameter | requested value | obtained value\n"); | |
188 | dolog ("format | %10d | %10d\n", req->fmt, obt->fmt); | |
189 | dolog ("channels | %10d | %10d\n", | |
190 | req->nchannels, obt->nchannels); | |
191 | dolog ("frequency | %10d | %10d\n", req->freq, obt->freq); | |
192 | dolog ("nfrags | %10d | %10d\n", req->nfrags, obt->nfrags); | |
193 | dolog ("fragsize | %10d | %10d\n", | |
194 | req->fragsize, obt->fragsize); | |
195 | } | |
196 | #endif | |
197 | ||
198 | static int oss_open (int in, struct oss_params *req, | |
199 | struct oss_params *obt, int *pfd) | |
200 | { | |
201 | int fd; | |
202 | int mmmmssss; | |
203 | audio_buf_info abinfo; | |
204 | int fmt, freq, nchannels; | |
205 | const char *dspname = in ? conf.devpath_in : conf.devpath_out; | |
206 | const char *typ = in ? "ADC" : "DAC"; | |
207 | ||
208 | fd = open (dspname, (in ? O_RDONLY : O_WRONLY) | O_NONBLOCK); | |
209 | if (-1 == fd) { | |
210 | oss_logerr2 (errno, typ, "Failed to open `%s'\n", dspname); | |
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)) { | |
219 | oss_logerr2 (errno, typ, "Failed to set sample size %d\n", req->fmt); | |
220 | goto err; | |
221 | } | |
222 | ||
223 | if (ioctl (fd, SNDCTL_DSP_CHANNELS, &nchannels)) { | |
224 | oss_logerr2 (errno, typ, "Failed to set number of channels %d\n", | |
225 | req->nchannels); | |
226 | goto err; | |
227 | } | |
228 | ||
229 | if (ioctl (fd, SNDCTL_DSP_SPEED, &freq)) { | |
230 | oss_logerr2 (errno, typ, "Failed to set frequency %d\n", req->freq); | |
231 | goto err; | |
232 | } | |
233 | ||
234 | if (ioctl (fd, SNDCTL_DSP_NONBLOCK)) { | |
235 | oss_logerr2 (errno, typ, "Failed to set non-blocking mode\n"); | |
236 | goto err; | |
237 | } | |
238 | ||
239 | mmmmssss = (req->nfrags << 16) | lsbindex (req->fragsize); | |
240 | if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss)) { | |
241 | oss_logerr2 (errno, typ, "Failed to set buffer length (%d, %d)\n", | |
242 | req->nfrags, req->fragsize); | |
243 | goto err; | |
244 | } | |
245 | ||
246 | if (ioctl (fd, in ? SNDCTL_DSP_GETISPACE : SNDCTL_DSP_GETOSPACE, &abinfo)) { | |
247 | oss_logerr2 (errno, typ, "Failed to get buffer length\n"); | |
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 | ||
258 | #ifdef DEBUG_MISMATCHES | |
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)) { | |
264 | dolog ("Audio parameters mismatch\n"); | |
265 | oss_dump_info (req, obt); | |
266 | } | |
267 | #endif | |
268 | ||
269 | #ifdef DEBUG | |
270 | oss_dump_info (req, obt); | |
271 | #endif | |
272 | return 0; | |
273 | ||
274 | err: | |
275 | oss_anal_close (&fd); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | static int oss_run_out (HWVoiceOut *hw) | |
280 | { | |
281 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
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; | |
288 | int bufsize; | |
289 | ||
290 | live = audio_pcm_hw_get_live_out (hw); | |
291 | if (!live) { | |
292 | return 0; | |
293 | } | |
294 | ||
295 | bufsize = hw->samples << hw->info.shift; | |
296 | ||
297 | if (oss->mmapped) { | |
298 | int bytes; | |
299 | ||
300 | err = ioctl (oss->fd, SNDCTL_DSP_GETOPTR, &cntinfo); | |
301 | if (err < 0) { | |
302 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); | |
303 | return 0; | |
304 | } | |
305 | ||
306 | if (cntinfo.ptr == oss->old_optr) { | |
307 | if (abs (hw->samples - live) < 64) { | |
308 | dolog ("warning: Overrun\n"); | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
313 | if (cntinfo.ptr > oss->old_optr) { | |
314 | bytes = cntinfo.ptr - oss->old_optr; | |
315 | } | |
316 | else { | |
317 | bytes = bufsize + cntinfo.ptr - oss->old_optr; | |
318 | } | |
319 | ||
320 | decr = audio_MIN (bytes >> hw->info.shift, live); | |
321 | } | |
322 | else { | |
323 | err = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &abinfo); | |
324 | if (err < 0) { | |
325 | oss_logerr (errno, "SNDCTL_DSP_GETOPTR failed\n"); | |
326 | return 0; | |
327 | } | |
328 | ||
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 [email protected]\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 | } | |
343 | return 0; | |
344 | } | |
345 | ||
346 | decr = audio_MIN (abinfo.bytes >> hw->info.shift, live); | |
347 | if (!decr) { | |
348 | return 0; | |
349 | } | |
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 | ||
358 | src = hw->mix_buf + rpos; | |
359 | dst = advance (oss->pcm_buf, rpos << hw->info.shift); | |
360 | ||
361 | hw->clip (dst, src, convert_samples); | |
362 | if (!oss->mmapped) { | |
363 | int written; | |
364 | ||
365 | written = write (oss->fd, dst, convert_samples << hw->info.shift); | |
366 | /* XXX: follow errno recommendations ? */ | |
367 | if (written == -1) { | |
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 | ); | |
374 | continue; | |
375 | } | |
376 | ||
377 | if (written != convert_samples << hw->info.shift) { | |
378 | int wsamples = written >> hw->info.shift; | |
379 | int wbytes = wsamples << hw->info.shift; | |
380 | if (wbytes != written) { | |
381 | dolog ("warning: Misaligned write %d (requested %d), " | |
382 | "alignment %d\n", | |
383 | wbytes, written, hw->info.align + 1); | |
384 | } | |
385 | decr -= wsamples; | |
386 | rpos = (rpos + wsamples) % hw->samples; | |
387 | break; | |
388 | } | |
389 | } | |
390 | ||
391 | rpos = (rpos + convert_samples) % hw->samples; | |
392 | samples -= convert_samples; | |
393 | } | |
394 | if (oss->mmapped) { | |
395 | oss->old_optr = cntinfo.ptr; | |
396 | } | |
397 | ||
398 | hw->rpos = rpos; | |
399 | return decr; | |
400 | } | |
401 | ||
402 | static void oss_fini_out (HWVoiceOut *hw) | |
403 | { | |
404 | int err; | |
405 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
406 | ||
407 | ldebug ("oss_fini\n"); | |
408 | oss_anal_close (&oss->fd); | |
409 | ||
410 | if (oss->pcm_buf) { | |
411 | if (oss->mmapped) { | |
412 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); | |
413 | if (err) { | |
414 | oss_logerr (errno, "Failed to unmap buffer %p, size %d\n", | |
415 | oss->pcm_buf, hw->samples << hw->info.shift); | |
416 | } | |
417 | } | |
418 | else { | |
419 | qemu_free (oss->pcm_buf); | |
420 | } | |
421 | oss->pcm_buf = NULL; | |
422 | } | |
423 | } | |
424 | ||
425 | static int oss_init_out (HWVoiceOut *hw, audsettings_t *as) | |
426 | { | |
427 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
428 | struct oss_params req, obt; | |
429 | int endianness; | |
430 | int err; | |
431 | int fd; | |
432 | audfmt_e effective_fmt; | |
433 | audsettings_t obt_as; | |
434 | ||
435 | oss->fd = -1; | |
436 | ||
437 | req.fmt = aud_to_ossfmt (as->fmt); | |
438 | req.freq = as->freq; | |
439 | req.nchannels = as->nchannels; | |
440 | req.fragsize = conf.fragsize; | |
441 | req.nfrags = conf.nfrags; | |
442 | ||
443 | if (oss_open (0, &req, &obt, &fd)) { | |
444 | return -1; | |
445 | } | |
446 | ||
447 | err = oss_to_audfmt (obt.fmt, &effective_fmt, &endianness); | |
448 | if (err) { | |
449 | oss_anal_close (&fd); | |
450 | return -1; | |
451 | } | |
452 | ||
453 | obt_as.freq = obt.freq; | |
454 | obt_as.nchannels = obt.nchannels; | |
455 | obt_as.fmt = effective_fmt; | |
456 | ||
457 | audio_pcm_init_info ( | |
458 | &hw->info, | |
459 | &obt_as, | |
460 | audio_need_to_swap_endian (endianness) | |
461 | ); | |
462 | oss->nfrags = obt.nfrags; | |
463 | oss->fragsize = obt.fragsize; | |
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; | |
471 | ||
472 | oss->mmapped = 0; | |
473 | if (conf.try_mmap) { | |
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 | ); | |
482 | if (oss->pcm_buf == MAP_FAILED) { | |
483 | oss_logerr (errno, "Failed to map %d bytes of DAC\n", | |
484 | hw->samples << hw->info.shift); | |
485 | } else { | |
486 | int err; | |
487 | int trig = 0; | |
488 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
489 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); | |
490 | } | |
491 | else { | |
492 | trig = PCM_ENABLE_OUTPUT; | |
493 | if (ioctl (fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
494 | oss_logerr ( | |
495 | errno, | |
496 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
497 | ); | |
498 | } | |
499 | else { | |
500 | oss->mmapped = 1; | |
501 | } | |
502 | } | |
503 | ||
504 | if (!oss->mmapped) { | |
505 | err = munmap (oss->pcm_buf, hw->samples << hw->info.shift); | |
506 | if (err) { | |
507 | oss_logerr (errno, "Failed to unmap buffer %p size %d\n", | |
508 | oss->pcm_buf, hw->samples << hw->info.shift); | |
509 | } | |
510 | } | |
511 | } | |
512 | } | |
513 | ||
514 | if (!oss->mmapped) { | |
515 | oss->pcm_buf = audio_calloc ( | |
516 | AUDIO_FUNC, | |
517 | hw->samples, | |
518 | 1 << hw->info.shift | |
519 | ); | |
520 | if (!oss->pcm_buf) { | |
521 | dolog ( | |
522 | "Could not allocate DAC buffer (%d samples, each %d bytes)\n", | |
523 | hw->samples, | |
524 | 1 << hw->info.shift | |
525 | ); | |
526 | oss_anal_close (&fd); | |
527 | return -1; | |
528 | } | |
529 | } | |
530 | ||
531 | oss->fd = fd; | |
532 | return 0; | |
533 | } | |
534 | ||
535 | static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...) | |
536 | { | |
537 | int trig; | |
538 | OSSVoiceOut *oss = (OSSVoiceOut *) hw; | |
539 | ||
540 | if (!oss->mmapped) { | |
541 | return 0; | |
542 | } | |
543 | ||
544 | switch (cmd) { | |
545 | case VOICE_ENABLE: | |
546 | ldebug ("enabling voice\n"); | |
547 | audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples); | |
548 | trig = PCM_ENABLE_OUTPUT; | |
549 | if (ioctl (oss->fd, SNDCTL_DSP_SETTRIGGER, &trig) < 0) { | |
550 | oss_logerr ( | |
551 | errno, | |
552 | "SNDCTL_DSP_SETTRIGGER PCM_ENABLE_OUTPUT failed\n" | |
553 | ); | |
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) { | |
562 | oss_logerr (errno, "SNDCTL_DSP_SETTRIGGER 0 failed\n"); | |
563 | return -1; | |
564 | } | |
565 | break; | |
566 | } | |
567 | return 0; | |
568 | } | |
569 | ||
570 | static int oss_init_in (HWVoiceIn *hw, audsettings_t *as) | |
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; | |
578 | audsettings_t obt_as; | |
579 | ||
580 | oss->fd = -1; | |
581 | ||
582 | req.fmt = aud_to_ossfmt (as->fmt); | |
583 | req.freq = as->freq; | |
584 | req.nchannels = as->nchannels; | |
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 | ||
597 | obt_as.freq = obt.freq; | |
598 | obt_as.nchannels = obt.nchannels; | |
599 | obt_as.fmt = effective_fmt; | |
600 | ||
601 | audio_pcm_init_info ( | |
602 | &hw->info, | |
603 | &obt_as, | |
604 | audio_need_to_swap_endian (endianness) | |
605 | ); | |
606 | oss->nfrags = obt.nfrags; | |
607 | oss->fragsize = obt.fragsize; | |
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); | |
616 | if (!oss->pcm_buf) { | |
617 | dolog ("Could not allocate ADC buffer (%d samples, each %d bytes)\n", | |
618 | hw->samples, 1 << hw->info.shift); | |
619 | oss_anal_close (&fd); | |
620 | return -1; | |
621 | } | |
622 | ||
623 | oss->fd = fd; | |
624 | return 0; | |
625 | } | |
626 | ||
627 | static 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 | ||
639 | static 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) { | |
677 | dolog ("warning: Misaligned read %zd (requested %d), " | |
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 | ||
710 | static int oss_read (SWVoiceIn *sw, void *buf, int size) | |
711 | { | |
712 | return audio_pcm_sw_read (sw, buf, size); | |
713 | } | |
714 | ||
715 | static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...) | |
716 | { | |
717 | (void) hw; | |
718 | (void) cmd; | |
719 | return 0; | |
720 | } | |
721 | ||
722 | static void *oss_audio_init (void) | |
723 | { | |
724 | return &conf; | |
725 | } | |
726 | ||
727 | static void oss_audio_fini (void *opaque) | |
728 | { | |
729 | (void) opaque; | |
730 | } | |
731 | ||
732 | static 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}, | |
743 | {"DEBUG", AUD_OPT_BOOL, &conf.debug, | |
744 | "Turn on some debugging messages", NULL, 0}, | |
745 | {NULL, 0, NULL, NULL, NULL, 0} | |
746 | }; | |
747 | ||
748 | static 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 | |
760 | }; | |
761 | ||
762 | struct 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) | |
774 | }; |