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