]> code.delx.au - pulseaudio/blob - src/modules/module-oss.c
consolidate close() calls to pa_close(), and make sure on every occasion that we...
[pulseaudio] / src / modules / module-oss.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 /* General power management rules:
26 *
27 * When SUSPENDED we close the audio device.
28 *
29 * We make no difference between IDLE and RUNNING in our handling.
30 *
31 * As long as we are in RUNNING/IDLE state we will *always* write data to
32 * the device. If none is avilable from the inputs, we write silence
33 * instead.
34 *
35 * If power should be saved on IDLE module-suspend-on-idle should be used.
36 *
37 */
38
39 /* TODO: handle restoring of volume after suspend */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #ifdef HAVE_SYS_MMAN_H
46 #include <sys/mman.h>
47 #endif
48
49 #include <sys/soundcard.h>
50 #include <sys/ioctl.h>
51 #include <stdlib.h>
52 #include <sys/stat.h>
53 #include <stdio.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <fcntl.h>
57 #include <unistd.h>
58 #include <limits.h>
59 #include <signal.h>
60
61 #include <pulse/xmalloc.h>
62 #include <pulse/util.h>
63
64 #include <pulsecore/core-error.h>
65 #include <pulsecore/thread.h>
66 #include <pulsecore/sink.h>
67 #include <pulsecore/source.h>
68 #include <pulsecore/module.h>
69 #include <pulsecore/sample-util.h>
70 #include <pulsecore/core-util.h>
71 #include <pulsecore/modargs.h>
72 #include <pulsecore/log.h>
73 #include <pulsecore/macro.h>
74 #include <pulsecore/thread-mq.h>
75 #include <pulsecore/rtpoll.h>
76
77 #include "oss-util.h"
78 #include "module-oss-symdef.h"
79
80 PA_MODULE_AUTHOR("Lennart Poettering")
81 PA_MODULE_DESCRIPTION("OSS Sink/Source")
82 PA_MODULE_VERSION(PACKAGE_VERSION)
83 PA_MODULE_USAGE(
84 "sink_name=<name for the sink> "
85 "source_name=<name for the source> "
86 "device=<OSS device> "
87 "record=<enable source?> "
88 "playback=<enable sink?> "
89 "format=<sample format> "
90 "channels=<number of channels> "
91 "rate=<sample rate> "
92 "fragments=<number of fragments> "
93 "fragment_size=<fragment size> "
94 "channel_map=<channel map> "
95 "mmap=<enable memory mapping?>")
96
97 #define DEFAULT_DEVICE "/dev/dsp"
98
99 struct userdata {
100 pa_core *core;
101 pa_module *module;
102 pa_sink *sink;
103 pa_source *source;
104
105 pa_thread *thread;
106 pa_thread_mq thread_mq;
107 pa_rtpoll *rtpoll;
108
109 char *device_name;
110
111 pa_memchunk memchunk;
112
113 size_t frame_size;
114 uint32_t in_fragment_size, out_fragment_size, in_nfrags, out_nfrags, in_hwbuf_size, out_hwbuf_size;
115 int use_getospace, use_getispace;
116 int use_getodelay;
117
118 int use_pcm_volume;
119 int use_input_volume;
120
121 int sink_suspended, source_suspended;
122
123 int fd;
124 int mode;
125
126 int nfrags, frag_size;
127
128 int use_mmap;
129 unsigned out_mmap_current, in_mmap_current;
130 void *in_mmap, *out_mmap;
131 pa_memblock **in_mmap_memblocks, **out_mmap_memblocks;
132
133 int in_mmap_saved_nfrags, out_mmap_saved_nfrags;
134
135 pa_rtpoll_item *rtpoll_item;
136 };
137
138 static const char* const valid_modargs[] = {
139 "sink_name",
140 "source_name",
141 "device",
142 "record",
143 "playback",
144 "fragments",
145 "fragment_size",
146 "format",
147 "rate",
148 "channels",
149 "channel_map",
150 "mmap",
151 NULL
152 };
153
154 static void trigger(struct userdata *u, int quick) {
155 int enable_bits = 0, zero = 0;
156
157 pa_assert(u);
158
159 if (u->fd < 0)
160 return;
161
162 /* pa_log_debug("trigger"); */
163
164 if (u->source && PA_SOURCE_OPENED(u->source->thread_info.state))
165 enable_bits |= PCM_ENABLE_INPUT;
166
167 if (u->sink && PA_SINK_OPENED(u->sink->thread_info.state))
168 enable_bits |= PCM_ENABLE_OUTPUT;
169
170 if (u->use_mmap) {
171
172 if (!quick)
173 ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &zero);
174
175 #ifdef SNDCTL_DSP_HALT
176 if (enable_bits == 0)
177 if (ioctl(u->fd, SNDCTL_DSP_HALT, NULL) < 0)
178 pa_log_warn("SNDCTL_DSP_HALT: %s", pa_cstrerror(errno));
179 #endif
180
181 if (ioctl(u->fd, SNDCTL_DSP_SETTRIGGER, &enable_bits) < 0)
182 pa_log_warn("SNDCTL_DSP_SETTRIGGER: %s", pa_cstrerror(errno));
183
184 if (u->sink && !(enable_bits & PCM_ENABLE_OUTPUT)) {
185 pa_log_debug("clearing playback buffer");
186 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &u->sink->sample_spec);
187 }
188
189 } else {
190
191 if (enable_bits)
192 if (ioctl(u->fd, SNDCTL_DSP_POST, NULL) < 0)
193 pa_log_warn("SNDCTL_DSP_POST: %s", pa_cstrerror(errno));
194
195 if (!quick) {
196 /*
197 * Some crappy drivers do not start the recording until we
198 * read something. Without this snippet, poll will never
199 * register the fd as ready.
200 */
201
202 if (u->source && PA_SOURCE_OPENED(u->source->thread_info.state)) {
203 uint8_t *buf = pa_xnew(uint8_t, u->in_fragment_size);
204 pa_read(u->fd, buf, u->in_fragment_size, NULL);
205 pa_xfree(buf);
206 }
207 }
208 }
209 }
210
211 static void mmap_fill_memblocks(struct userdata *u, unsigned n) {
212 pa_assert(u);
213 pa_assert(u->out_mmap_memblocks);
214
215 /* pa_log("Mmmap writing %u blocks", n); */
216
217 while (n > 0) {
218 pa_memchunk chunk;
219
220 if (u->out_mmap_memblocks[u->out_mmap_current])
221 pa_memblock_unref_fixed(u->out_mmap_memblocks[u->out_mmap_current]);
222
223 chunk.memblock = u->out_mmap_memblocks[u->out_mmap_current] =
224 pa_memblock_new_fixed(
225 u->core->mempool,
226 (uint8_t*) u->out_mmap + u->out_fragment_size * u->out_mmap_current,
227 u->out_fragment_size,
228 1);
229
230 chunk.length = pa_memblock_get_length(chunk.memblock);
231 chunk.index = 0;
232
233 pa_sink_render_into_full(u->sink, &chunk);
234
235 u->out_mmap_current++;
236 while (u->out_mmap_current >= u->out_nfrags)
237 u->out_mmap_current -= u->out_nfrags;
238
239 n--;
240 }
241 }
242
243 static int mmap_write(struct userdata *u) {
244 struct count_info info;
245
246 pa_assert(u);
247 pa_assert(u->sink);
248
249 /* pa_log("Mmmap writing..."); */
250
251 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
252 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
253 return -1;
254 }
255
256 info.blocks += u->out_mmap_saved_nfrags;
257 u->out_mmap_saved_nfrags = 0;
258
259 if (info.blocks > 0)
260 mmap_fill_memblocks(u, info.blocks);
261
262 return info.blocks;
263 }
264
265 static void mmap_post_memblocks(struct userdata *u, unsigned n) {
266 pa_assert(u);
267 pa_assert(u->in_mmap_memblocks);
268
269 /* pa_log("Mmmap reading %u blocks", n); */
270
271 while (n > 0) {
272 pa_memchunk chunk;
273
274 if (!u->in_mmap_memblocks[u->in_mmap_current]) {
275
276 chunk.memblock = u->in_mmap_memblocks[u->in_mmap_current] =
277 pa_memblock_new_fixed(
278 u->core->mempool,
279 (uint8_t*) u->in_mmap + u->in_fragment_size*u->in_mmap_current,
280 u->in_fragment_size,
281 1);
282
283 chunk.length = pa_memblock_get_length(chunk.memblock);
284 chunk.index = 0;
285
286 pa_source_post(u->source, &chunk);
287 }
288
289 u->in_mmap_current++;
290 while (u->in_mmap_current >= u->in_nfrags)
291 u->in_mmap_current -= u->in_nfrags;
292
293 n--;
294 }
295 }
296
297 static void mmap_clear_memblocks(struct userdata*u, unsigned n) {
298 unsigned i = u->in_mmap_current;
299
300 pa_assert(u);
301 pa_assert(u->in_mmap_memblocks);
302
303 if (n > u->in_nfrags)
304 n = u->in_nfrags;
305
306 while (n > 0) {
307 if (u->in_mmap_memblocks[i]) {
308 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
309 u->in_mmap_memblocks[i] = NULL;
310 }
311
312 i++;
313 while (i >= u->in_nfrags)
314 i -= u->in_nfrags;
315
316 n--;
317 }
318 }
319
320 static int mmap_read(struct userdata *u) {
321 struct count_info info;
322 pa_assert(u);
323 pa_assert(u->source);
324
325 /* pa_log("Mmmap reading..."); */
326
327 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
328 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
329 return -1;
330 }
331
332 info.blocks += u->in_mmap_saved_nfrags;
333 u->in_mmap_saved_nfrags = 0;
334
335 if (info.blocks > 0) {
336 mmap_post_memblocks(u, info.blocks);
337 mmap_clear_memblocks(u, u->in_nfrags/2);
338 }
339
340 return info.blocks;
341 }
342
343 static pa_usec_t mmap_sink_get_latency(struct userdata *u) {
344 struct count_info info;
345 size_t bpos, n;
346
347 pa_assert(u);
348
349 if (ioctl(u->fd, SNDCTL_DSP_GETOPTR, &info) < 0) {
350 pa_log("SNDCTL_DSP_GETOPTR: %s", pa_cstrerror(errno));
351 return 0;
352 }
353
354 u->out_mmap_saved_nfrags += info.blocks;
355
356 bpos = ((u->out_mmap_current + u->out_mmap_saved_nfrags) * u->out_fragment_size) % u->out_hwbuf_size;
357
358 if (bpos <= (size_t) info.ptr)
359 n = u->out_hwbuf_size - (info.ptr - bpos);
360 else
361 n = bpos - info.ptr;
362
363 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->out_fragment_size, u->out_fragments); */
364
365 return pa_bytes_to_usec(n, &u->sink->sample_spec);
366 }
367
368 static pa_usec_t mmap_source_get_latency(struct userdata *u) {
369 struct count_info info;
370 size_t bpos, n;
371
372 pa_assert(u);
373
374 if (ioctl(u->fd, SNDCTL_DSP_GETIPTR, &info) < 0) {
375 pa_log("SNDCTL_DSP_GETIPTR: %s", pa_cstrerror(errno));
376 return 0;
377 }
378
379 u->in_mmap_saved_nfrags += info.blocks;
380 bpos = ((u->in_mmap_current + u->in_mmap_saved_nfrags) * u->in_fragment_size) % u->in_hwbuf_size;
381
382 if (bpos <= (size_t) info.ptr)
383 n = info.ptr - bpos;
384 else
385 n = u->in_hwbuf_size - bpos + info.ptr;
386
387 /* pa_log("n = %u, bpos = %u, ptr = %u, total=%u, fragsize = %u, n_frags = %u\n", n, bpos, (unsigned) info.ptr, total, u->in_fragment_size, u->in_fragments); */
388
389 return pa_bytes_to_usec(n, &u->source->sample_spec);
390 }
391
392 static pa_usec_t io_sink_get_latency(struct userdata *u) {
393 pa_usec_t r = 0;
394
395 pa_assert(u);
396
397 if (u->use_getodelay) {
398 int arg;
399
400 if (ioctl(u->fd, SNDCTL_DSP_GETODELAY, &arg) < 0) {
401 pa_log_info("Device doesn't support SNDCTL_DSP_GETODELAY: %s", pa_cstrerror(errno));
402 u->use_getodelay = 0;
403 } else
404 r = pa_bytes_to_usec(arg, &u->sink->sample_spec);
405
406 }
407
408 if (!u->use_getodelay && u->use_getospace) {
409 struct audio_buf_info info;
410
411 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
412 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
413 u->use_getospace = 0;
414 } else
415 r = pa_bytes_to_usec(info.bytes, &u->sink->sample_spec);
416 }
417
418 if (u->memchunk.memblock)
419 r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
420
421 return r;
422 }
423
424
425 static pa_usec_t io_source_get_latency(struct userdata *u) {
426 pa_usec_t r = 0;
427
428 pa_assert(u);
429
430 if (u->use_getispace) {
431 struct audio_buf_info info;
432
433 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
434 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
435 u->use_getispace = 0;
436 } else
437 r = pa_bytes_to_usec(info.bytes, &u->source->sample_spec);
438 }
439
440 return r;
441 }
442
443 static int suspend(struct userdata *u) {
444 pa_assert(u);
445 pa_assert(u->fd >= 0);
446
447 pa_log_info("Suspending...");
448
449 if (u->out_mmap_memblocks) {
450 unsigned i;
451 for (i = 0; i < u->out_nfrags; i++)
452 if (u->out_mmap_memblocks[i]) {
453 pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
454 u->out_mmap_memblocks[i] = NULL;
455 }
456 }
457
458 if (u->in_mmap_memblocks) {
459 unsigned i;
460 for (i = 0; i < u->in_nfrags; i++)
461 if (u->in_mmap_memblocks[i]) {
462 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
463 u->in_mmap_memblocks[i] = NULL;
464 }
465 }
466
467 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
468 munmap(u->in_mmap, u->in_hwbuf_size);
469 u->in_mmap = NULL;
470 }
471
472 if (u->out_mmap && u->out_mmap != MAP_FAILED) {
473 munmap(u->out_mmap, u->out_hwbuf_size);
474 u->out_mmap = NULL;
475 }
476
477 /* Let's suspend */
478 ioctl(u->fd, SNDCTL_DSP_SYNC, NULL);
479 pa_close(u->fd);
480 u->fd = -1;
481
482 if (u->rtpoll_item) {
483 pa_rtpoll_item_free(u->rtpoll_item);
484 u->rtpoll_item = NULL;
485 }
486
487 pa_log_info("Device suspended...");
488
489 return 0;
490 }
491
492 static int unsuspend(struct userdata *u) {
493 int m;
494 pa_sample_spec ss, *ss_original;
495 int frag_size, in_frag_size, out_frag_size;
496 int in_nfrags, out_nfrags;
497 struct audio_buf_info info;
498 struct pollfd *pollfd;
499
500 pa_assert(u);
501 pa_assert(u->fd < 0);
502
503 m = u->mode;
504
505 pa_log_info("Trying resume...");
506
507 if ((u->fd = pa_oss_open(u->device_name, &m, NULL)) < 0) {
508 pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno));
509 return -1;
510
511 if (m != u->mode)
512 pa_log_warn("Resume failed, couldn't open device with original access mode.");
513 goto fail;
514 }
515
516 if (u->nfrags >= 2 && u->frag_size >= 1)
517 if (pa_oss_set_fragments(u->fd, u->nfrags, u->frag_size) < 0) {
518 pa_log_warn("Resume failed, couldn't set original fragment settings.");
519 goto fail;
520 }
521
522 ss = *(ss_original = u->sink ? &u->sink->sample_spec : &u->source->sample_spec);
523 if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, ss_original)) {
524 pa_log_warn("Resume failed, couldn't set original sample format settings.");
525 goto fail;
526 }
527
528 if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
529 pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
530 goto fail;
531 }
532
533 in_frag_size = out_frag_size = frag_size;
534 in_nfrags = out_nfrags = u->nfrags;
535
536 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
537 in_frag_size = info.fragsize;
538 in_nfrags = info.fragstotal;
539 }
540
541 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
542 out_frag_size = info.fragsize;
543 out_nfrags = info.fragstotal;
544 }
545
546 if ((u->source && (in_frag_size != (int) u->in_fragment_size || in_nfrags != (int) u->in_nfrags)) ||
547 (u->sink && (out_frag_size != (int) u->out_fragment_size || out_nfrags != (int) u->out_nfrags))) {
548 pa_log_warn("Resume failed, input fragment settings don't match.");
549 goto fail;
550 }
551
552 if (u->use_mmap) {
553 if (u->source) {
554 if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
555 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
556 goto fail;
557 }
558 }
559
560 if (u->sink) {
561 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
562 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
563 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
564 munmap(u->in_mmap, u->in_hwbuf_size);
565 u->in_mmap = NULL;
566 }
567
568 goto fail;
569 }
570
571 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
572 }
573 }
574
575 u->out_mmap_current = u->in_mmap_current = 0;
576 u->out_mmap_saved_nfrags = u->in_mmap_saved_nfrags = 0;
577
578 pa_assert(!u->rtpoll_item);
579
580 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, 1);
581 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
582 pollfd->fd = u->fd;
583 pollfd->events = 0;
584 pollfd->revents = 0;
585
586 pa_log_info("Resumed successfully...");
587
588 return 0;
589
590 fail:
591 pa_close(u->fd);
592 u->fd = -1;
593 return -1;
594 }
595
596 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
597 struct userdata *u = PA_SINK(o)->userdata;
598 int do_trigger = 0, ret, quick = 1;
599
600 switch (code) {
601
602 case PA_SINK_MESSAGE_GET_LATENCY: {
603 pa_usec_t r = 0;
604
605 if (u->fd >= 0) {
606 if (u->use_mmap)
607 r = mmap_sink_get_latency(u);
608 else
609 r = io_sink_get_latency(u);
610 }
611
612 *((pa_usec_t*) data) = r;
613
614 return 0;
615 }
616
617 case PA_SINK_MESSAGE_SET_STATE:
618
619 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
620
621 case PA_SINK_SUSPENDED:
622 pa_assert(PA_SINK_OPENED(u->sink->thread_info.state));
623
624 if (!u->source || u->source_suspended) {
625 if (suspend(u) < 0)
626 return -1;
627 }
628
629 do_trigger = 1;
630
631 u->sink_suspended = 1;
632 break;
633
634 case PA_SINK_IDLE:
635 case PA_SINK_RUNNING:
636
637 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
638
639 if (!u->source || u->source_suspended) {
640 if (unsuspend(u) < 0)
641 return -1;
642 quick = 0;
643 }
644
645 do_trigger = 1;
646
647 u->out_mmap_current = 0;
648 u->out_mmap_saved_nfrags = 0;
649
650 u->sink_suspended = 0;
651 }
652
653 break;
654
655 case PA_SINK_UNLINKED:
656 case PA_SINK_INIT:
657 ;
658 }
659
660 break;
661
662 case PA_SINK_MESSAGE_SET_VOLUME:
663
664 if (u->use_pcm_volume && u->fd >= 0) {
665
666 if (pa_oss_set_pcm_volume(u->fd, &u->sink->sample_spec, ((pa_cvolume*) data)) < 0) {
667 pa_log_info("Device doesn't support setting mixer settings: %s", pa_cstrerror(errno));
668 u->use_pcm_volume = 0;
669 } else
670 return 0;
671 }
672
673 break;
674
675 case PA_SINK_MESSAGE_GET_VOLUME:
676
677 if (u->use_pcm_volume && u->fd >= 0) {
678
679 if (pa_oss_get_pcm_volume(u->fd, &u->sink->sample_spec, ((pa_cvolume*) data)) < 0) {
680 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
681 u->use_pcm_volume = 0;
682 } else
683 return 0;
684 }
685
686 break;
687 }
688
689 ret = pa_sink_process_msg(o, code, data, offset, chunk);
690
691 if (do_trigger)
692 trigger(u, quick);
693
694 return ret;
695 }
696
697 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
698 struct userdata *u = PA_SOURCE(o)->userdata;
699 int do_trigger = 0, ret, quick = 1;
700
701 switch (code) {
702
703 case PA_SOURCE_MESSAGE_GET_LATENCY: {
704 pa_usec_t r = 0;
705
706 if (u->fd >= 0) {
707 if (u->use_mmap)
708 r = mmap_source_get_latency(u);
709 else
710 r = io_source_get_latency(u);
711 }
712
713 *((pa_usec_t*) data) = r;
714 return 0;
715 }
716
717 case PA_SOURCE_MESSAGE_SET_STATE:
718
719 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
720 case PA_SOURCE_SUSPENDED:
721 pa_assert(PA_SOURCE_OPENED(u->source->thread_info.state));
722
723 if (!u->sink || u->sink_suspended) {
724 if (suspend(u) < 0)
725 return -1;
726 }
727
728 do_trigger = 1;
729
730 u->source_suspended = 1;
731 break;
732
733 case PA_SOURCE_IDLE:
734 case PA_SOURCE_RUNNING:
735
736 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
737
738 if (!u->sink || u->sink_suspended) {
739 if (unsuspend(u) < 0)
740 return -1;
741 quick = 0;
742 }
743
744 do_trigger = 1;
745
746 u->in_mmap_current = 0;
747 u->in_mmap_saved_nfrags = 0;
748
749 u->source_suspended = 0;
750 }
751 break;
752
753 case PA_SOURCE_UNLINKED:
754 case PA_SOURCE_INIT:
755 ;
756
757 }
758 break;
759
760 case PA_SOURCE_MESSAGE_SET_VOLUME:
761
762 if (u->use_input_volume && u->fd >= 0) {
763
764 if (pa_oss_set_input_volume(u->fd, &u->source->sample_spec, ((pa_cvolume*) data)) < 0) {
765 pa_log_info("Device doesn't support setting mixer settings: %s", pa_cstrerror(errno));
766 u->use_input_volume = 0;
767 } else
768 return 0;
769 }
770
771 break;
772
773 case PA_SOURCE_MESSAGE_GET_VOLUME:
774
775 if (u->use_input_volume && u->fd >= 0) {
776
777 if (pa_oss_get_input_volume(u->fd, &u->source->sample_spec, ((pa_cvolume*) data)) < 0) {
778 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
779 u->use_input_volume = 0;
780 } else
781 return 0;
782 }
783
784 break;
785 }
786
787 ret = pa_source_process_msg(o, code, data, offset, chunk);
788
789 if (do_trigger)
790 trigger(u, quick);
791
792 return ret;
793 }
794
795 static void thread_func(void *userdata) {
796 struct userdata *u = userdata;
797 int write_type = 0, read_type = 0;
798 unsigned short revents = 0;
799
800 pa_assert(u);
801
802 pa_log_debug("Thread starting up");
803
804 if (u->core->high_priority)
805 pa_make_realtime();
806
807 pa_thread_mq_install(&u->thread_mq);
808 pa_rtpoll_install(u->rtpoll);
809
810 trigger(u, 0);
811
812 for (;;) {
813 int ret;
814
815 /* pa_log("loop"); */
816
817 /* Render some data and write it to the dsp */
818
819 if (u->sink && u->sink->thread_info.state != PA_SINK_UNLINKED && u->fd >= 0 && (revents & POLLOUT)) {
820
821 if (u->use_mmap) {
822
823 if ((ret = mmap_write(u)) < 0)
824 goto fail;
825
826 revents &= ~POLLOUT;
827
828 if (ret > 0)
829 continue;
830
831 } else {
832 ssize_t l;
833 int loop = 0;
834
835 l = u->out_fragment_size;
836
837 if (u->use_getospace) {
838 audio_buf_info info;
839
840 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
841 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
842 u->use_getospace = 0;
843 } else {
844 if (info.bytes >= l) {
845 l = (info.bytes/l)*l;
846 loop = 1;
847 }
848 }
849 }
850
851 do {
852 void *p;
853 ssize_t t;
854
855 pa_assert(l > 0);
856
857 if (u->memchunk.length <= 0)
858 pa_sink_render(u->sink, l, &u->memchunk);
859
860 pa_assert(u->memchunk.length > 0);
861
862 p = pa_memblock_acquire(u->memchunk.memblock);
863 t = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
864 pa_memblock_release(u->memchunk.memblock);
865
866 /* pa_log("wrote %i bytes of %u", t, l); */
867
868 pa_assert(t != 0);
869
870 if (t < 0) {
871
872 if (errno == EINTR)
873 continue;
874
875 else if (errno == EAGAIN) {
876 pa_log_debug("EAGAIN");
877
878 revents &= ~POLLOUT;
879 break;
880
881 } else {
882 pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
883 goto fail;
884 }
885
886 } else {
887
888 u->memchunk.index += t;
889 u->memchunk.length -= t;
890
891 if (u->memchunk.length <= 0) {
892 pa_memblock_unref(u->memchunk.memblock);
893 pa_memchunk_reset(&u->memchunk);
894 }
895
896 l -= t;
897
898 revents &= ~POLLOUT;
899 }
900
901 } while (loop && l > 0);
902
903 continue;
904 }
905 }
906
907 /* Try to read some data and pass it on to the source driver */
908
909 if (u->source && u->source->thread_info.state != PA_SOURCE_UNLINKED && u->fd >= 0 && ((revents & POLLIN))) {
910
911 if (u->use_mmap) {
912
913 if ((ret = mmap_read(u)) < 0)
914 goto fail;
915
916 revents &= ~POLLIN;
917
918 if (ret > 0)
919 continue;
920
921 } else {
922
923 void *p;
924 ssize_t l;
925 pa_memchunk memchunk;
926 int loop = 0;
927
928 l = u->in_fragment_size;
929
930 if (u->use_getispace) {
931 audio_buf_info info;
932
933 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
934 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
935 u->use_getispace = 0;
936 } else {
937 if (info.bytes >= l) {
938 l = (info.bytes/l)*l;
939 loop = 1;
940 }
941 }
942 }
943
944 do {
945 ssize_t t, k;
946
947 pa_assert(l > 0);
948
949 memchunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
950
951 k = pa_memblock_get_length(memchunk.memblock);
952
953 if (k > l)
954 k = l;
955
956 k = (k/u->frame_size)*u->frame_size;
957
958 p = pa_memblock_acquire(memchunk.memblock);
959 t = pa_read(u->fd, p, k, &read_type);
960 pa_memblock_release(memchunk.memblock);
961
962 pa_assert(t != 0); /* EOF cannot happen */
963
964 /* pa_log("read %i bytes of %u", t, l); */
965
966 if (t < 0) {
967 pa_memblock_unref(memchunk.memblock);
968
969 if (errno == EINTR)
970 continue;
971
972 else if (errno == EAGAIN) {
973 pa_log_debug("EAGAIN");
974
975 revents &= ~POLLIN;
976 break;
977
978 } else {
979 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
980 goto fail;
981 }
982
983 } else {
984 memchunk.index = 0;
985 memchunk.length = t;
986
987 pa_source_post(u->source, &memchunk);
988 pa_memblock_unref(memchunk.memblock);
989
990 l -= t;
991
992 revents &= ~POLLIN;
993 }
994 } while (loop && l > 0);
995
996 continue;
997 }
998 }
999
1000 /* pa_log("loop2"); */
1001
1002 /* Now give the sink inputs some to time to process their data */
1003 if (u->sink) {
1004 if ((ret = pa_sink_process_inputs(u->sink)) < 0)
1005 goto fail;
1006 if (ret > 0)
1007 continue;
1008 }
1009
1010 /* Now give the source outputs some to time to process their data */
1011 if (u->source) {
1012 if ((ret = pa_source_process_outputs(u->source)) < 0)
1013 goto fail;
1014 if (ret > 0)
1015 continue;
1016 }
1017
1018 /* Check whether there is a message for us to process */
1019 if ((ret = pa_thread_mq_process(&u->thread_mq) < 0))
1020 goto finish;
1021 if (ret > 0)
1022 continue;
1023
1024 if (u->fd >= 0) {
1025 struct pollfd *pollfd;
1026
1027 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1028 pollfd->events =
1029 ((u->source && PA_SOURCE_OPENED(u->source->thread_info.state)) ? POLLIN : 0) |
1030 ((u->sink && PA_SINK_OPENED(u->sink->thread_info.state)) ? POLLOUT : 0);
1031 }
1032
1033 /* Hmm, nothing to do. Let's sleep */
1034 if (pa_rtpoll_run(u->rtpoll, 1) < 0) {
1035 pa_log("poll() failed: %s", pa_cstrerror(errno));
1036 goto fail;
1037 }
1038
1039 if (u->fd >= 0) {
1040 struct pollfd *pollfd;
1041
1042 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1043
1044 if (pollfd->revents & ~(POLLOUT|POLLIN)) {
1045 pa_log("DSP shutdown.");
1046 goto fail;
1047 }
1048
1049 revents = pollfd->revents;
1050 } else
1051 revents = 0;
1052 }
1053
1054 fail:
1055 /* We have to continue processing messages until we receive the
1056 * SHUTDOWN message */
1057 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1058 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1059
1060 finish:
1061 pa_log_debug("Thread shutting down");
1062 }
1063
1064 int pa__init(pa_module*m) {
1065
1066 struct audio_buf_info info;
1067 struct userdata *u = NULL;
1068 const char *dev;
1069 int fd = -1;
1070 int nfrags, frag_size;
1071 int mode, caps;
1072 int record = 1, playback = 1, use_mmap = 1;
1073 pa_sample_spec ss;
1074 pa_channel_map map;
1075 pa_modargs *ma = NULL;
1076 char hwdesc[64], *t;
1077 const char *name;
1078 int namereg_fail;
1079 struct pollfd *pollfd;
1080
1081 pa_assert(m);
1082
1083 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1084 pa_log("Failed to parse module arguments.");
1085 goto fail;
1086 }
1087
1088 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
1089 pa_log("record= and playback= expect numeric argument.");
1090 goto fail;
1091 }
1092
1093 if (!playback && !record) {
1094 pa_log("Neither playback nor record enabled for device.");
1095 goto fail;
1096 }
1097
1098 mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
1099
1100 ss = m->core->default_sample_spec;
1101 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
1102 pa_log("Failed to parse sample specification or channel map");
1103 goto fail;
1104 }
1105
1106 nfrags = m->core->default_n_fragments;
1107 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
1108 if (frag_size <= 0)
1109 frag_size = pa_frame_size(&ss);
1110
1111 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
1112 pa_log("Failed to parse fragments arguments");
1113 goto fail;
1114 }
1115
1116 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1117 pa_log("Failed to parse mmap argument.");
1118 goto fail;
1119 }
1120
1121 if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
1122 goto fail;
1123
1124 if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
1125 pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
1126 use_mmap = 0;
1127 }
1128
1129 if (use_mmap && mode == O_WRONLY) {
1130 pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
1131 use_mmap = 0;
1132 }
1133
1134 if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
1135 pa_log_info("Hardware name is '%s'.", hwdesc);
1136 else
1137 hwdesc[0] = 0;
1138
1139 pa_log_info("Device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
1140
1141 if (nfrags >= 2 && frag_size >= 1)
1142 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
1143 goto fail;
1144
1145 if (pa_oss_auto_format(fd, &ss) < 0)
1146 goto fail;
1147
1148 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
1149 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
1150 goto fail;
1151 }
1152 pa_assert(frag_size > 0);
1153
1154 u = pa_xnew0(struct userdata, 1);
1155 u->core = m->core;
1156 u->module = m;
1157 m->userdata = u;
1158 u->fd = fd;
1159 u->use_getospace = u->use_getispace = 1;
1160 u->use_getodelay = 1;
1161 u->use_input_volume = u->use_pcm_volume = 1;
1162 u->mode = mode;
1163 u->frame_size = pa_frame_size(&ss);
1164 u->device_name = pa_xstrdup(dev);
1165 u->in_nfrags = u->out_nfrags = u->nfrags = nfrags;
1166 u->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size;
1167 u->use_mmap = use_mmap;
1168 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
1169 u->rtpoll = pa_rtpoll_new();
1170 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, u->thread_mq.inq);
1171 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, 1);
1172 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1173 pollfd->fd = fd;
1174 pollfd->events = 0;
1175 pollfd->revents = 0;
1176
1177 if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
1178 pa_log_info("Input -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1179 u->in_fragment_size = info.fragsize;
1180 u->in_nfrags = info.fragstotal;
1181 u->use_getispace = 1;
1182 }
1183
1184 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
1185 pa_log_info("Output -- %u fragments of size %u.", info.fragstotal, info.fragsize);
1186 u->out_fragment_size = info.fragsize;
1187 u->out_nfrags = info.fragstotal;
1188 u->use_getospace = 1;
1189 }
1190
1191 u->in_hwbuf_size = u->in_nfrags * u->in_fragment_size;
1192 u->out_hwbuf_size = u->out_nfrags * u->out_fragment_size;
1193
1194 if (mode != O_WRONLY) {
1195 char *name_buf = NULL;
1196
1197 if (use_mmap) {
1198 if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1199 pa_log_warn("mmap(PROT_READ) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1200 use_mmap = u->use_mmap = 0;
1201 u->in_mmap = NULL;
1202 } else
1203 pa_log_debug("Successfully mmap()ed input buffer.");
1204 }
1205
1206 if ((name = pa_modargs_get_value(ma, "source_name", NULL)))
1207 namereg_fail = 1;
1208 else {
1209 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
1210 namereg_fail = 0;
1211 }
1212
1213 u->source = pa_source_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
1214 pa_xfree(name_buf);
1215 if (!u->source) {
1216 pa_log("Failed to create source object");
1217 goto fail;
1218 }
1219
1220 u->source->parent.process_msg = source_process_msg;
1221 u->source->userdata = u;
1222
1223 pa_source_set_module(u->source, m);
1224 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1225 pa_source_set_rtpoll(u->source, u->rtpoll);
1226 pa_source_set_description(u->source, t = pa_sprintf_malloc(
1227 "OSS PCM on %s%s%s%s%s",
1228 dev,
1229 hwdesc[0] ? " (" : "",
1230 hwdesc[0] ? hwdesc : "",
1231 hwdesc[0] ? ")" : "",
1232 use_mmap ? " via DMA" : ""));
1233 pa_xfree(t);
1234 u->source->flags = PA_SOURCE_HARDWARE|PA_SOURCE_CAN_SUSPEND|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL;
1235 u->source->refresh_volume = 1;
1236
1237 if (use_mmap)
1238 u->in_mmap_memblocks = pa_xnew0(pa_memblock*, u->in_nfrags);
1239 }
1240
1241 if (mode != O_RDONLY) {
1242 char *name_buf = NULL;
1243
1244 if (use_mmap) {
1245 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1246 if (mode == O_RDWR) {
1247 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1248 mode = O_WRONLY;
1249 goto go_on;
1250 } else {
1251 pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1252 u->use_mmap = use_mmap = 0;
1253 u->out_mmap = NULL;
1254 }
1255 } else {
1256 pa_log_debug("Successfully mmap()ed output buffer.");
1257 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
1258 }
1259 }
1260
1261 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1262 namereg_fail = 1;
1263 else {
1264 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
1265 namereg_fail = 0;
1266 }
1267
1268 u->sink = pa_sink_new(m->core, __FILE__, name, namereg_fail, &ss, &map);
1269 pa_xfree(name_buf);
1270 if (!u->sink) {
1271 pa_log("Failed to create sink object");
1272 goto fail;
1273 }
1274
1275 u->sink->parent.process_msg = sink_process_msg;
1276 u->sink->userdata = u;
1277
1278 pa_sink_set_module(u->sink, m);
1279 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1280 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1281 pa_sink_set_description(u->sink, t = pa_sprintf_malloc(
1282 "OSS PCM on %s%s%s%s%s",
1283 dev,
1284 hwdesc[0] ? " (" : "",
1285 hwdesc[0] ? hwdesc : "",
1286 hwdesc[0] ? ")" : "",
1287 use_mmap ? " via DMA" : ""));
1288 pa_xfree(t);
1289 u->sink->flags = PA_SINK_HARDWARE|PA_SINK_CAN_SUSPEND|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL;
1290 u->sink->refresh_volume = 1;
1291
1292 if (use_mmap)
1293 u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags);
1294 }
1295
1296 go_on:
1297
1298 pa_assert(u->source || u->sink);
1299
1300 pa_memchunk_reset(&u->memchunk);
1301
1302 if (!(u->thread = pa_thread_new(thread_func, u))) {
1303 pa_log("Failed to create thread.");
1304 goto fail;
1305 }
1306
1307 /* Read mixer settings */
1308 if (u->source)
1309 pa_asyncmsgq_send(u->thread_mq.inq, PA_MSGOBJECT(u->source), PA_SOURCE_MESSAGE_GET_VOLUME, &u->source->volume, 0, NULL);
1310 if (u->sink)
1311 pa_asyncmsgq_send(u->thread_mq.inq, PA_MSGOBJECT(u->sink), PA_SINK_MESSAGE_GET_VOLUME, &u->sink->volume, 0, NULL);
1312
1313 if (u->sink)
1314 pa_sink_put(u->sink);
1315 if (u->source)
1316 pa_source_put(u->source);
1317
1318 pa_modargs_free(ma);
1319
1320 return 0;
1321
1322 fail:
1323
1324 if (u)
1325 pa__done(m);
1326 else if (fd >= 0)
1327 pa_close(fd);
1328
1329 if (ma)
1330 pa_modargs_free(ma);
1331
1332 return -1;
1333 }
1334
1335 void pa__done(pa_module*m) {
1336 struct userdata *u;
1337
1338 pa_assert(m);
1339
1340 if (!(u = m->userdata))
1341 return;
1342
1343 if (u->sink)
1344 pa_sink_unlink(u->sink);
1345
1346 if (u->source)
1347 pa_source_unlink(u->source);
1348
1349 if (u->thread) {
1350 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1351 pa_thread_free(u->thread);
1352 }
1353
1354 pa_thread_mq_done(&u->thread_mq);
1355
1356 if (u->sink)
1357 pa_sink_unref(u->sink);
1358
1359 if (u->source)
1360 pa_source_unref(u->source);
1361
1362 if (u->memchunk.memblock)
1363 pa_memblock_unref(u->memchunk.memblock);
1364
1365 if (u->rtpoll_item)
1366 pa_rtpoll_item_free(u->rtpoll_item);
1367
1368 if (u->rtpoll)
1369 pa_rtpoll_free(u->rtpoll);
1370
1371 if (u->out_mmap_memblocks) {
1372 unsigned i;
1373 for (i = 0; i < u->out_nfrags; i++)
1374 if (u->out_mmap_memblocks[i])
1375 pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
1376 pa_xfree(u->out_mmap_memblocks);
1377 }
1378
1379 if (u->in_mmap_memblocks) {
1380 unsigned i;
1381 for (i = 0; i < u->in_nfrags; i++)
1382 if (u->in_mmap_memblocks[i])
1383 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
1384 pa_xfree(u->in_mmap_memblocks);
1385 }
1386
1387 if (u->in_mmap && u->in_mmap != MAP_FAILED)
1388 munmap(u->in_mmap, u->in_hwbuf_size);
1389
1390 if (u->out_mmap && u->out_mmap != MAP_FAILED)
1391 munmap(u->out_mmap, u->out_hwbuf_size);
1392
1393 if (u->fd >= 0)
1394 pa_close(u->fd);
1395
1396 pa_xfree(u->device_name);
1397
1398 pa_xfree(u);
1399 }