]> code.delx.au - pulseaudio/blob - src/modules/module-oss.c
Rework module-combine to work with glitch-free core; add new max_request field to...
[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 unsuspend(struct userdata *u) {
512 int m;
513 pa_sample_spec ss, *ss_original;
514 int frag_size, in_frag_size, out_frag_size;
515 int in_nfrags, out_nfrags;
516 struct audio_buf_info info;
517
518 pa_assert(u);
519 pa_assert(u->fd < 0);
520
521 m = u->mode;
522
523 pa_log_info("Trying resume...");
524
525 if ((u->fd = pa_oss_open(u->device_name, &m, NULL)) < 0) {
526 pa_log_warn("Resume failed, device busy (%s)", pa_cstrerror(errno));
527 return -1;
528
529 if (m != u->mode)
530 pa_log_warn("Resume failed, couldn't open device with original access mode.");
531 goto fail;
532 }
533
534 if (u->nfrags >= 2 && u->frag_size >= 1)
535 if (pa_oss_set_fragments(u->fd, u->nfrags, u->frag_size) < 0) {
536 pa_log_warn("Resume failed, couldn't set original fragment settings.");
537 goto fail;
538 }
539
540 ss = *(ss_original = u->sink ? &u->sink->sample_spec : &u->source->sample_spec);
541 if (pa_oss_auto_format(u->fd, &ss) < 0 || !pa_sample_spec_equal(&ss, ss_original)) {
542 pa_log_warn("Resume failed, couldn't set original sample format settings.");
543 goto fail;
544 }
545
546 if (ioctl(u->fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
547 pa_log_warn("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
548 goto fail;
549 }
550
551 in_frag_size = out_frag_size = frag_size;
552 in_nfrags = out_nfrags = u->nfrags;
553
554 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) >= 0) {
555 in_frag_size = info.fragsize;
556 in_nfrags = info.fragstotal;
557 }
558
559 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) >= 0) {
560 out_frag_size = info.fragsize;
561 out_nfrags = info.fragstotal;
562 }
563
564 if ((u->source && (in_frag_size != (int) u->in_fragment_size || in_nfrags != (int) u->in_nfrags)) ||
565 (u->sink && (out_frag_size != (int) u->out_fragment_size || out_nfrags != (int) u->out_nfrags))) {
566 pa_log_warn("Resume failed, input fragment settings don't match.");
567 goto fail;
568 }
569
570 if (u->use_mmap) {
571 if (u->source) {
572 if ((u->in_mmap = mmap(NULL, u->in_hwbuf_size, PROT_READ, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
573 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
574 goto fail;
575 }
576 }
577
578 if (u->sink) {
579 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, u->fd, 0)) == MAP_FAILED) {
580 pa_log("Resume failed, mmap(): %s", pa_cstrerror(errno));
581 if (u->in_mmap && u->in_mmap != MAP_FAILED) {
582 munmap(u->in_mmap, u->in_hwbuf_size);
583 u->in_mmap = NULL;
584 }
585
586 goto fail;
587 }
588
589 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
590 }
591 }
592
593 u->out_mmap_current = u->in_mmap_current = 0;
594 u->out_mmap_saved_nfrags = u->in_mmap_saved_nfrags = 0;
595
596 pa_assert(!u->rtpoll_item);
597
598 build_pollfd(u);
599
600 if (u->sink)
601 pa_sink_get_volume(u->sink);
602 if (u->source)
603 pa_source_get_volume(u->source);
604
605 pa_log_info("Resumed successfully...");
606
607 return 0;
608
609 fail:
610 pa_close(u->fd);
611 u->fd = -1;
612 return -1;
613 }
614
615 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
616 struct userdata *u = PA_SINK(o)->userdata;
617 int ret;
618 pa_bool_t do_trigger = FALSE, quick = TRUE;
619
620 switch (code) {
621
622 case PA_SINK_MESSAGE_GET_LATENCY: {
623 pa_usec_t r = 0;
624
625 if (u->fd >= 0) {
626 if (u->use_mmap)
627 r = mmap_sink_get_latency(u);
628 else
629 r = io_sink_get_latency(u);
630 }
631
632 *((pa_usec_t*) data) = r;
633
634 return 0;
635 }
636
637 case PA_SINK_MESSAGE_SET_STATE:
638
639 switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) {
640
641 case PA_SINK_SUSPENDED:
642 pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state));
643
644 if (!u->source || u->source_suspended) {
645 if (suspend(u) < 0)
646 return -1;
647 }
648
649 do_trigger = TRUE;
650
651 u->sink_suspended = TRUE;
652 break;
653
654 case PA_SINK_IDLE:
655 case PA_SINK_RUNNING:
656
657 if (u->sink->thread_info.state == PA_SINK_INIT) {
658 do_trigger = TRUE;
659 quick = u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state);
660 }
661
662 if (u->sink->thread_info.state == PA_SINK_SUSPENDED) {
663
664 if (!u->source || u->source_suspended) {
665 if (unsuspend(u) < 0)
666 return -1;
667 quick = FALSE;
668 }
669
670 do_trigger = TRUE;
671
672 u->out_mmap_current = 0;
673 u->out_mmap_saved_nfrags = 0;
674
675 u->sink_suspended = FALSE;
676 }
677
678 break;
679
680 case PA_SINK_UNLINKED:
681 case PA_SINK_INIT:
682 ;
683 }
684
685 break;
686
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 ret;
700 int do_trigger = FALSE, quick = TRUE;
701
702 switch (code) {
703
704 case PA_SOURCE_MESSAGE_GET_LATENCY: {
705 pa_usec_t r = 0;
706
707 if (u->fd >= 0) {
708 if (u->use_mmap)
709 r = mmap_source_get_latency(u);
710 else
711 r = io_source_get_latency(u);
712 }
713
714 *((pa_usec_t*) data) = r;
715 return 0;
716 }
717
718 case PA_SOURCE_MESSAGE_SET_STATE:
719
720 switch ((pa_source_state_t) PA_PTR_TO_UINT(data)) {
721 case PA_SOURCE_SUSPENDED:
722 pa_assert(PA_SOURCE_IS_OPENED(u->source->thread_info.state));
723
724 if (!u->sink || u->sink_suspended) {
725 if (suspend(u) < 0)
726 return -1;
727 }
728
729 do_trigger = TRUE;
730
731 u->source_suspended = TRUE;
732 break;
733
734 case PA_SOURCE_IDLE:
735 case PA_SOURCE_RUNNING:
736
737 if (u->source->thread_info.state == PA_SOURCE_INIT) {
738 do_trigger = TRUE;
739 quick = u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state);
740 }
741
742 if (u->source->thread_info.state == PA_SOURCE_SUSPENDED) {
743
744 if (!u->sink || u->sink_suspended) {
745 if (unsuspend(u) < 0)
746 return -1;
747 quick = FALSE;
748 }
749
750 do_trigger = TRUE;
751
752 u->in_mmap_current = 0;
753 u->in_mmap_saved_nfrags = 0;
754
755 u->source_suspended = FALSE;
756 }
757 break;
758
759 case PA_SOURCE_UNLINKED:
760 case PA_SOURCE_INIT:
761 ;
762
763 }
764 break;
765
766 }
767
768 ret = pa_source_process_msg(o, code, data, offset, chunk);
769
770 if (do_trigger)
771 trigger(u, quick);
772
773 return ret;
774 }
775
776 static int sink_get_volume(pa_sink *s) {
777 struct userdata *u;
778 int r;
779
780 pa_assert_se(u = s->userdata);
781
782 pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
783
784 if (u->mixer_devmask & SOUND_MASK_VOLUME)
785 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->volume)) >= 0)
786 return r;
787
788 if (u->mixer_devmask & SOUND_MASK_PCM)
789 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->volume)) >= 0)
790 return r;
791
792 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
793 return -1;
794 }
795
796 static int sink_set_volume(pa_sink *s) {
797 struct userdata *u;
798 int r;
799
800 pa_assert_se(u = s->userdata);
801
802 pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
803
804 if (u->mixer_devmask & SOUND_MASK_VOLUME)
805 if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->volume)) >= 0)
806 return r;
807
808 if (u->mixer_devmask & SOUND_MASK_PCM)
809 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->volume)) >= 0)
810 return r;
811
812 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
813 return -1;
814 }
815
816 static int source_get_volume(pa_source *s) {
817 struct userdata *u;
818 int r;
819
820 pa_assert_se(u = s->userdata);
821
822 pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
823
824 if (u->mixer_devmask & SOUND_MASK_IGAIN)
825 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->volume)) >= 0)
826 return r;
827
828 if (u->mixer_devmask & SOUND_MASK_RECLEV)
829 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->volume)) >= 0)
830 return r;
831
832 pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
833 return -1;
834 }
835
836 static int source_set_volume(pa_source *s) {
837 struct userdata *u;
838 int r;
839
840 pa_assert_se(u = s->userdata);
841
842 pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
843
844 if (u->mixer_devmask & SOUND_MASK_IGAIN)
845 if ((r = pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->volume)) >= 0)
846 return r;
847
848 if (u->mixer_devmask & SOUND_MASK_RECLEV)
849 if ((r = pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->volume)) >= 0)
850 return r;
851
852 pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
853 return -1;
854 }
855
856 static void thread_func(void *userdata) {
857 struct userdata *u = userdata;
858 int write_type = 0, read_type = 0;
859 unsigned short revents = 0;
860
861 pa_assert(u);
862
863 pa_log_debug("Thread starting up");
864
865 if (u->core->realtime_scheduling)
866 pa_make_realtime(u->core->realtime_priority);
867
868 pa_thread_mq_install(&u->thread_mq);
869 pa_rtpoll_install(u->rtpoll);
870
871 for (;;) {
872 int ret;
873
874 /* pa_log("loop"); */
875
876 /* Render some data and write it to the dsp */
877
878 if (u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && ((revents & POLLOUT) || u->use_mmap || u->use_getospace)) {
879
880 if (u->use_mmap) {
881
882 if ((ret = mmap_write(u)) < 0)
883 goto fail;
884
885 revents &= ~POLLOUT;
886
887 if (ret > 0)
888 continue;
889
890 } else {
891 ssize_t l;
892 pa_bool_t loop = FALSE, work_done = FALSE;
893
894 l = u->out_fragment_size;
895
896 if (u->use_getospace) {
897 audio_buf_info info;
898
899 if (ioctl(u->fd, SNDCTL_DSP_GETOSPACE, &info) < 0) {
900 pa_log_info("Device doesn't support SNDCTL_DSP_GETOSPACE: %s", pa_cstrerror(errno));
901 u->use_getospace = FALSE;
902 } else {
903 l = info.bytes;
904
905 /* We loop only if GETOSPACE worked and we
906 * actually *know* that we can write more than
907 * one fragment at a time */
908 loop = TRUE;
909 }
910 }
911
912 /* Round down to multiples of the fragment size,
913 * because OSS needs that (at least some versions
914 * do) */
915 l = (l/u->out_fragment_size) * u->out_fragment_size;
916
917 /* Hmm, so poll() signalled us that we can read
918 * something, but GETOSPACE told us there was nothing?
919 * Hmm, make the best of it, try to read some data, to
920 * avoid spinning forever. */
921 if (l <= 0 && (revents & POLLOUT)) {
922 l = u->out_fragment_size;
923 loop = FALSE;
924 }
925
926 while (l > 0) {
927 void *p;
928 ssize_t t;
929
930 if (u->memchunk.length <= 0)
931 pa_sink_render(u->sink, l, &u->memchunk);
932
933 pa_assert(u->memchunk.length > 0);
934
935 p = pa_memblock_acquire(u->memchunk.memblock);
936 t = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
937 pa_memblock_release(u->memchunk.memblock);
938
939 /* pa_log("wrote %i bytes of %u", t, l); */
940
941 pa_assert(t != 0);
942
943 if (t < 0) {
944
945 if (errno == EINTR)
946 continue;
947
948 else if (errno == EAGAIN) {
949 pa_log_debug("EAGAIN");
950
951 revents &= ~POLLOUT;
952 break;
953
954 } else {
955 pa_log("Failed to write data to DSP: %s", pa_cstrerror(errno));
956 goto fail;
957 }
958
959 } else {
960
961 u->memchunk.index += t;
962 u->memchunk.length -= t;
963
964 if (u->memchunk.length <= 0) {
965 pa_memblock_unref(u->memchunk.memblock);
966 pa_memchunk_reset(&u->memchunk);
967 }
968
969 l -= t;
970
971 revents &= ~POLLOUT;
972 work_done = TRUE;
973 }
974
975 if (!loop)
976 break;
977 }
978
979 if (work_done)
980 continue;
981 }
982 }
983
984 /* Try to read some data and pass it on to the source driver. */
985
986 if (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) && ((revents & POLLIN) || u->use_mmap || u->use_getispace)) {
987
988 if (u->use_mmap) {
989
990 if ((ret = mmap_read(u)) < 0)
991 goto fail;
992
993 revents &= ~POLLIN;
994
995 if (ret > 0)
996 continue;
997
998 } else {
999
1000 void *p;
1001 ssize_t l;
1002 pa_memchunk memchunk;
1003 pa_bool_t loop = FALSE, work_done = FALSE;
1004
1005 l = u->in_fragment_size;
1006
1007 if (u->use_getispace) {
1008 audio_buf_info info;
1009
1010 if (ioctl(u->fd, SNDCTL_DSP_GETISPACE, &info) < 0) {
1011 pa_log_info("Device doesn't support SNDCTL_DSP_GETISPACE: %s", pa_cstrerror(errno));
1012 u->use_getispace = FALSE;
1013 } else {
1014 l = info.bytes;
1015 loop = TRUE;
1016 }
1017 }
1018
1019 l = (l/u->in_fragment_size) * u->in_fragment_size;
1020
1021 if (l <= 0 && (revents & POLLIN)) {
1022 l = u->in_fragment_size;
1023 loop = FALSE;
1024 }
1025
1026 while (l > 0) {
1027 ssize_t t, k;
1028
1029 pa_assert(l > 0);
1030
1031 memchunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1);
1032
1033 k = pa_memblock_get_length(memchunk.memblock);
1034
1035 if (k > l)
1036 k = l;
1037
1038 k = (k/u->frame_size)*u->frame_size;
1039
1040 p = pa_memblock_acquire(memchunk.memblock);
1041 t = pa_read(u->fd, p, k, &read_type);
1042 pa_memblock_release(memchunk.memblock);
1043
1044 pa_assert(t != 0); /* EOF cannot happen */
1045
1046 /* pa_log("read %i bytes of %u", t, l); */
1047
1048 if (t < 0) {
1049 pa_memblock_unref(memchunk.memblock);
1050
1051 if (errno == EINTR)
1052 continue;
1053
1054 else if (errno == EAGAIN) {
1055 pa_log_debug("EAGAIN");
1056
1057 revents &= ~POLLIN;
1058 break;
1059
1060 } else {
1061 pa_log("Failed to read data from DSP: %s", pa_cstrerror(errno));
1062 goto fail;
1063 }
1064
1065 } else {
1066 memchunk.index = 0;
1067 memchunk.length = t;
1068
1069 pa_source_post(u->source, &memchunk);
1070 pa_memblock_unref(memchunk.memblock);
1071
1072 l -= t;
1073
1074 revents &= ~POLLIN;
1075 work_done = TRUE;
1076 }
1077
1078 if (!loop)
1079 break;
1080 }
1081
1082 if (work_done)
1083 continue;
1084 }
1085 }
1086
1087 /* pa_log("loop2 revents=%i", revents); */
1088
1089 if (u->rtpoll_item) {
1090 struct pollfd *pollfd;
1091
1092 pa_assert(u->fd >= 0);
1093
1094 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1095 pollfd->events =
1096 ((u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state)) ? POLLIN : 0) |
1097 ((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state)) ? POLLOUT : 0);
1098 }
1099
1100 /* Hmm, nothing to do. Let's sleep */
1101 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
1102 goto fail;
1103
1104 if (ret == 0)
1105 goto finish;
1106
1107 if (u->rtpoll_item) {
1108 struct pollfd *pollfd;
1109
1110 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
1111
1112 if (pollfd->revents & ~(POLLOUT|POLLIN)) {
1113 pa_log("DSP shutdown.");
1114 goto fail;
1115 }
1116
1117 revents = pollfd->revents;
1118 } else
1119 revents = 0;
1120 }
1121
1122 fail:
1123 /* If this was no regular exit from the loop we have to continue
1124 * processing messages until we received PA_MESSAGE_SHUTDOWN */
1125 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
1126 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
1127
1128 finish:
1129 pa_log_debug("Thread shutting down");
1130 }
1131
1132 int pa__init(pa_module*m) {
1133
1134 struct audio_buf_info info;
1135 struct userdata *u = NULL;
1136 const char *dev;
1137 int fd = -1;
1138 int nfrags, frag_size;
1139 int mode, caps;
1140 pa_bool_t record = TRUE, playback = TRUE, use_mmap = TRUE;
1141 pa_sample_spec ss;
1142 pa_channel_map map;
1143 pa_modargs *ma = NULL;
1144 char hwdesc[64];
1145 const char *name;
1146 pa_bool_t namereg_fail;
1147 pa_sink_new_data sink_new_data;
1148 pa_source_new_data source_new_data;
1149
1150 pa_assert(m);
1151
1152 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1153 pa_log("Failed to parse module arguments.");
1154 goto fail;
1155 }
1156
1157 if (pa_modargs_get_value_boolean(ma, "record", &record) < 0 || pa_modargs_get_value_boolean(ma, "playback", &playback) < 0) {
1158 pa_log("record= and playback= expect boolean argument.");
1159 goto fail;
1160 }
1161
1162 if (!playback && !record) {
1163 pa_log("Neither playback nor record enabled for device.");
1164 goto fail;
1165 }
1166
1167 mode = (playback && record) ? O_RDWR : (playback ? O_WRONLY : (record ? O_RDONLY : 0));
1168
1169 ss = m->core->default_sample_spec;
1170 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_OSS) < 0) {
1171 pa_log("Failed to parse sample specification or channel map");
1172 goto fail;
1173 }
1174
1175 nfrags = m->core->default_n_fragments;
1176 frag_size = pa_usec_to_bytes(m->core->default_fragment_size_msec*1000, &ss);
1177 if (frag_size <= 0)
1178 frag_size = pa_frame_size(&ss);
1179
1180 if (pa_modargs_get_value_s32(ma, "fragments", &nfrags) < 0 || pa_modargs_get_value_s32(ma, "fragment_size", &frag_size) < 0) {
1181 pa_log("Failed to parse fragments arguments");
1182 goto fail;
1183 }
1184
1185 if (pa_modargs_get_value_boolean(ma, "mmap", &use_mmap) < 0) {
1186 pa_log("Failed to parse mmap argument.");
1187 goto fail;
1188 }
1189
1190 if ((fd = pa_oss_open(dev = pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), &mode, &caps)) < 0)
1191 goto fail;
1192
1193 if (use_mmap && (!(caps & DSP_CAP_MMAP) || !(caps & DSP_CAP_TRIGGER))) {
1194 pa_log_info("OSS device not mmap capable, falling back to UNIX read/write mode.");
1195 use_mmap = 0;
1196 }
1197
1198 if (use_mmap && mode == O_WRONLY) {
1199 pa_log_info("Device opened for playback only, cannot do memory mapping, falling back to UNIX write() mode.");
1200 use_mmap = 0;
1201 }
1202
1203 if (pa_oss_get_hw_description(dev, hwdesc, sizeof(hwdesc)) >= 0)
1204 pa_log_info("Hardware name is '%s'.", hwdesc);
1205 else
1206 hwdesc[0] = 0;
1207
1208 pa_log_info("Device opened in %s mode.", mode == O_WRONLY ? "O_WRONLY" : (mode == O_RDONLY ? "O_RDONLY" : "O_RDWR"));
1209
1210 if (nfrags >= 2 && frag_size >= 1)
1211 if (pa_oss_set_fragments(fd, nfrags, frag_size) < 0)
1212 goto fail;
1213
1214 if (pa_oss_auto_format(fd, &ss) < 0)
1215 goto fail;
1216
1217 if (ioctl(fd, SNDCTL_DSP_GETBLKSIZE, &frag_size) < 0) {
1218 pa_log("SNDCTL_DSP_GETBLKSIZE: %s", pa_cstrerror(errno));
1219 goto fail;
1220 }
1221 pa_assert(frag_size > 0);
1222
1223 u = pa_xnew0(struct userdata, 1);
1224 u->core = m->core;
1225 u->module = m;
1226 m->userdata = u;
1227 u->fd = fd;
1228 u->mixer_fd = -1;
1229 u->use_getospace = u->use_getispace = TRUE;
1230 u->use_getodelay = TRUE;
1231 u->mode = mode;
1232 u->frame_size = pa_frame_size(&ss);
1233 u->device_name = pa_xstrdup(dev);
1234 u->in_nfrags = u->out_nfrags = u->nfrags = nfrags;
1235 u->out_fragment_size = u->in_fragment_size = u->frag_size = frag_size;
1236 u->use_mmap = use_mmap;
1237 u->rtpoll = pa_rtpoll_new();
1238 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
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 = TRUE;
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 = TRUE;
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 = FALSE;
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 = TRUE;
1273 else {
1274 name = name_buf = pa_sprintf_malloc("oss_input.%s", pa_path_get_filename(dev));
1275 namereg_fail = FALSE;
1276 }
1277
1278 pa_source_new_data_init(&source_new_data);
1279 source_new_data.driver = __FILE__;
1280 source_new_data.module = m;
1281 pa_source_new_data_set_name(&source_new_data, name);
1282 source_new_data.namereg_fail = namereg_fail;
1283 pa_source_new_data_set_sample_spec(&source_new_data, &ss);
1284 pa_source_new_data_set_channel_map(&source_new_data, &map);
1285 pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
1286 pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_API, "oss");
1287 pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
1288 pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
1289 pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->in_hwbuf_size));
1290 pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->in_fragment_size));
1291
1292 u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
1293 pa_source_new_data_done(&source_new_data);
1294 pa_xfree(name_buf);
1295
1296 if (!u->source) {
1297 pa_log("Failed to create source object");
1298 goto fail;
1299 }
1300
1301 u->source->parent.process_msg = source_process_msg;
1302 u->source->userdata = u;
1303
1304 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
1305 pa_source_set_rtpoll(u->source, u->rtpoll);
1306 u->source->refresh_volume = TRUE;
1307
1308 if (use_mmap)
1309 u->in_mmap_memblocks = pa_xnew0(pa_memblock*, u->in_nfrags);
1310 }
1311
1312 if (mode != O_RDONLY) {
1313 char *name_buf = NULL;
1314
1315 if (use_mmap) {
1316 if ((u->out_mmap = mmap(NULL, u->out_hwbuf_size, PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
1317 if (mode == O_RDWR) {
1318 pa_log_debug("mmap() failed for input. Changing to O_WRONLY mode.");
1319 mode = O_WRONLY;
1320 goto go_on;
1321 } else {
1322 pa_log_warn("mmap(PROT_WRITE) failed, reverting to non-mmap mode: %s", pa_cstrerror(errno));
1323 u->use_mmap = use_mmap = FALSE;
1324 u->out_mmap = NULL;
1325 }
1326 } else {
1327 pa_log_debug("Successfully mmap()ed output buffer.");
1328 pa_silence_memory(u->out_mmap, u->out_hwbuf_size, &ss);
1329 }
1330 }
1331
1332 if ((name = pa_modargs_get_value(ma, "sink_name", NULL)))
1333 namereg_fail = TRUE;
1334 else {
1335 name = name_buf = pa_sprintf_malloc("oss_output.%s", pa_path_get_filename(dev));
1336 namereg_fail = FALSE;
1337 }
1338
1339 pa_sink_new_data_init(&sink_new_data);
1340 sink_new_data.driver = __FILE__;
1341 sink_new_data.module = m;
1342 pa_sink_new_data_set_name(&sink_new_data, name);
1343 sink_new_data.namereg_fail = namereg_fail;
1344 pa_sink_new_data_set_sample_spec(&sink_new_data, &ss);
1345 pa_sink_new_data_set_channel_map(&sink_new_data, &map);
1346 pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_STRING, dev);
1347 pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_API, "oss");
1348 pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, hwdesc[0] ? hwdesc : dev);
1349 pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, use_mmap ? "mmap" : "serial");
1350 pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->out_hwbuf_size));
1351 pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->out_fragment_size));
1352
1353 u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
1354 pa_sink_new_data_done(&sink_new_data);
1355 pa_xfree(name_buf);
1356
1357 if (!u->sink) {
1358 pa_log("Failed to create sink object");
1359 goto fail;
1360 }
1361
1362 u->sink->parent.process_msg = sink_process_msg;
1363 u->sink->userdata = u;
1364
1365 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
1366 pa_sink_set_rtpoll(u->sink, u->rtpoll);
1367 u->sink->refresh_volume = TRUE;
1368
1369 u->sink->thread_info.max_request = u->out_hwbuf_size;
1370
1371 if (use_mmap)
1372 u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags);
1373 }
1374
1375 if ((u->mixer_fd = pa_oss_open_mixer_for_device(u->device_name)) >= 0) {
1376 pa_bool_t do_close = TRUE;
1377 u->mixer_devmask = 0;
1378
1379 if (ioctl(fd, SOUND_MIXER_READ_DEVMASK, &u->mixer_devmask) < 0)
1380 pa_log_warn("SOUND_MIXER_READ_DEVMASK failed: %s", pa_cstrerror(errno));
1381
1382 else {
1383 if (u->sink && (u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM))) {
1384 pa_log_debug("Found hardware mixer track for playback.");
1385 u->sink->flags |= PA_SINK_HW_VOLUME_CTRL;
1386 u->sink->get_volume = sink_get_volume;
1387 u->sink->set_volume = sink_set_volume;
1388 do_close = FALSE;
1389 }
1390
1391 if (u->source && (u->mixer_devmask & (SOUND_MASK_RECLEV|SOUND_MASK_IGAIN))) {
1392 pa_log_debug("Found hardware mixer track for recording.");
1393 u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL;
1394 u->source->get_volume = source_get_volume;
1395 u->source->set_volume = source_set_volume;
1396 do_close = FALSE;
1397 }
1398 }
1399
1400 if (do_close) {
1401 pa_close(u->mixer_fd);
1402 u->mixer_fd = -1;
1403 }
1404 }
1405
1406 go_on:
1407
1408 pa_assert(u->source || u->sink);
1409
1410 pa_memchunk_reset(&u->memchunk);
1411
1412 if (!(u->thread = pa_thread_new(thread_func, u))) {
1413 pa_log("Failed to create thread.");
1414 goto fail;
1415 }
1416
1417 /* Read mixer settings */
1418 if (u->sink) {
1419 if (sink_new_data.volume_is_set) {
1420 if (u->sink->set_volume)
1421 u->sink->set_volume(u->sink);
1422 } else {
1423 if (u->sink->get_volume)
1424 u->sink->get_volume(u->sink);
1425 }
1426 }
1427
1428 if (u->source) {
1429 if (source_new_data.volume_is_set) {
1430 if (u->source->set_volume)
1431 u->source->set_volume(u->source);
1432 } else {
1433 if (u->source->get_volume)
1434 u->source->get_volume(u->source);
1435 }
1436 }
1437
1438 if (u->sink)
1439 pa_sink_put(u->sink);
1440 if (u->source)
1441 pa_source_put(u->source);
1442
1443 pa_modargs_free(ma);
1444
1445 return 0;
1446
1447 fail:
1448
1449 if (u)
1450 pa__done(m);
1451 else if (fd >= 0)
1452 pa_close(fd);
1453
1454 if (ma)
1455 pa_modargs_free(ma);
1456
1457 return -1;
1458 }
1459
1460 void pa__done(pa_module*m) {
1461 struct userdata *u;
1462
1463 pa_assert(m);
1464
1465 if (!(u = m->userdata))
1466 return;
1467
1468 if (u->sink)
1469 pa_sink_unlink(u->sink);
1470
1471 if (u->source)
1472 pa_source_unlink(u->source);
1473
1474 if (u->thread) {
1475 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
1476 pa_thread_free(u->thread);
1477 }
1478
1479 pa_thread_mq_done(&u->thread_mq);
1480
1481 if (u->sink)
1482 pa_sink_unref(u->sink);
1483
1484 if (u->source)
1485 pa_source_unref(u->source);
1486
1487 if (u->memchunk.memblock)
1488 pa_memblock_unref(u->memchunk.memblock);
1489
1490 if (u->rtpoll_item)
1491 pa_rtpoll_item_free(u->rtpoll_item);
1492
1493 if (u->rtpoll)
1494 pa_rtpoll_free(u->rtpoll);
1495
1496 if (u->out_mmap_memblocks) {
1497 unsigned i;
1498 for (i = 0; i < u->out_nfrags; i++)
1499 if (u->out_mmap_memblocks[i])
1500 pa_memblock_unref_fixed(u->out_mmap_memblocks[i]);
1501 pa_xfree(u->out_mmap_memblocks);
1502 }
1503
1504 if (u->in_mmap_memblocks) {
1505 unsigned i;
1506 for (i = 0; i < u->in_nfrags; i++)
1507 if (u->in_mmap_memblocks[i])
1508 pa_memblock_unref_fixed(u->in_mmap_memblocks[i]);
1509 pa_xfree(u->in_mmap_memblocks);
1510 }
1511
1512 if (u->in_mmap && u->in_mmap != MAP_FAILED)
1513 munmap(u->in_mmap, u->in_hwbuf_size);
1514
1515 if (u->out_mmap && u->out_mmap != MAP_FAILED)
1516 munmap(u->out_mmap, u->out_hwbuf_size);
1517
1518 if (u->fd >= 0)
1519 pa_close(u->fd);
1520
1521 if (u->mixer_fd >= 0)
1522 pa_close(u->mixer_fd);
1523
1524 pa_xfree(u->device_name);
1525
1526 pa_xfree(u);
1527 }