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