4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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
29 #include <sys/soundcard.h>
30 #include <sys/ioctl.h>
35 #include <sys/types.h>
39 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/macro.h>
47 int pa_oss_open(const char *device
, int *mode
, int* pcaps
) {
53 pa_assert(*mode
== O_RDWR
|| *mode
== O_RDONLY
|| *mode
== O_WRONLY
);
58 if (*mode
== O_RDWR
) {
59 if ((fd
= open(device
, O_RDWR
|O_NDELAY
|O_NOCTTY
)) >= 0) {
60 ioctl(fd
, SNDCTL_DSP_SETDUPLEX
, 0);
62 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, pcaps
) < 0) {
63 pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno
));
67 if (*pcaps
& DSP_CAP_DUPLEX
)
70 pa_log_warn("'%s' doesn't support full duplex", device
);
75 if ((fd
= open(device
, (*mode
= O_WRONLY
)|O_NDELAY
|O_NOCTTY
)) < 0) {
76 if ((fd
= open(device
, (*mode
= O_RDONLY
)|O_NDELAY
|O_NOCTTY
)) < 0) {
77 pa_log("open('%s'): %s", device
, pa_cstrerror(errno
));
82 if ((fd
= open(device
, *mode
|O_NDELAY
|O_NOCTTY
)) < 0) {
83 pa_log("open('%s'): %s", device
, pa_cstrerror(errno
));
90 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, pcaps
) < 0) {
91 pa_log("SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno
));
97 pa_log_debug("capabilities:%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
98 *pcaps
& DSP_CAP_BATCH
? " BATCH" : "",
100 *pcaps
& DSP_CAP_BIND
? " BIND" : "",
104 *pcaps
& DSP_CAP_COPROC
? " COPROC" : "",
105 *pcaps
& DSP_CAP_DUPLEX
? " DUPLEX" : "",
106 #ifdef DSP_CAP_FREERATE
107 *pcaps
& DSP_CAP_FREERATE
? " FREERATE" : "",
112 *pcaps
& DSP_CAP_INPUT
? " INPUT" : "",
116 *pcaps
& DSP_CAP_MMAP
? " MMAP" : "",
118 *pcaps
& DSP_CAP_MODEM
? " MODEM" : "",
123 *pcaps
& DSP_CAP_MULTI
? " MULTI" : "",
127 #ifdef DSP_CAP_OUTPUT
128 *pcaps
& DSP_CAP_OUTPUT
? " OUTPUT" : "",
132 *pcaps
& DSP_CAP_REALTIME
? " REALTIME" : "",
133 #ifdef DSP_CAP_SHADOW
134 *pcaps
& DSP_CAP_SHADOW
? " SHADOW" : "",
138 #ifdef DSP_CAP_VIRTUAL
139 *pcaps
& DSP_CAP_VIRTUAL
? " VIRTUAL" : "",
143 *pcaps
& DSP_CAP_TRIGGER
? " TRIGGER" : "");
145 pa_make_fd_cloexec(fd
);
155 int pa_oss_auto_format(int fd
, pa_sample_spec
*ss
) {
156 int format
, channels
, speed
, reqformat
;
157 pa_sample_format_t orig_format
;
159 static const int format_trans
[PA_SAMPLE_MAX
] = {
160 [PA_SAMPLE_U8
] = AFMT_U8
,
161 [PA_SAMPLE_ALAW
] = AFMT_A_LAW
,
162 [PA_SAMPLE_ULAW
] = AFMT_MU_LAW
,
163 [PA_SAMPLE_S16LE
] = AFMT_S16_LE
,
164 [PA_SAMPLE_S16BE
] = AFMT_S16_BE
,
165 [PA_SAMPLE_FLOAT32LE
] = AFMT_QUERY
, /* not supported */
166 [PA_SAMPLE_FLOAT32BE
] = AFMT_QUERY
, /* not supported */
172 orig_format
= ss
->format
;
174 reqformat
= format
= format_trans
[ss
->format
];
175 if (reqformat
== AFMT_QUERY
|| ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= reqformat
) {
176 format
= AFMT_S16_NE
;
177 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= AFMT_S16_NE
) {
178 int f
= AFMT_S16_NE
== AFMT_S16_LE
? AFMT_S16_BE
: AFMT_S16_LE
;
180 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= f
) {
182 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= AFMT_U8
) {
183 pa_log("SNDCTL_DSP_SETFMT: %s", format
!= AFMT_U8
? "No supported sample format" : pa_cstrerror(errno
));
186 ss
->format
= PA_SAMPLE_U8
;
188 ss
->format
= f
== AFMT_S16_LE
? PA_SAMPLE_S16LE
: PA_SAMPLE_S16BE
;
190 ss
->format
= PA_SAMPLE_S16NE
;
193 if (orig_format
!= ss
->format
)
194 pa_log_warn("device doesn't support sample format %s, changed to %s.",
195 pa_sample_format_to_string(orig_format
),
196 pa_sample_format_to_string(ss
->format
));
198 channels
= ss
->channels
;
199 if (ioctl(fd
, SNDCTL_DSP_CHANNELS
, &channels
) < 0) {
200 pa_log("SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno
));
203 pa_assert(channels
> 0);
205 if (ss
->channels
!= channels
) {
206 pa_log_warn("device doesn't support %i channels, using %i channels.", ss
->channels
, channels
);
207 ss
->channels
= channels
;
211 if (ioctl(fd
, SNDCTL_DSP_SPEED
, &speed
) < 0) {
212 pa_log("SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno
));
215 pa_assert(speed
> 0);
217 if (ss
->rate
!= (unsigned) speed
) {
218 pa_log_warn("device doesn't support %i Hz, changed to %i Hz.", ss
->rate
, speed
);
220 /* If the sample rate deviates too much, we need to resample */
221 if (speed
< ss
->rate
*.95 || speed
> ss
->rate
*1.05)
228 static int simple_log2(int v
) {
240 int pa_oss_set_fragments(int fd
, int nfrags
, int frag_size
) {
242 arg
= ((int) nfrags
<< 16) | simple_log2(frag_size
);
244 if (ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &arg
) < 0) {
245 pa_log("SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno
));
252 int pa_oss_get_volume(int fd
, int mixer
, const pa_sample_spec
*ss
, pa_cvolume
*volume
) {
253 char cv
[PA_CVOLUME_SNPRINT_MAX
];
260 if (ioctl(fd
, mixer
, &vol
) < 0)
263 pa_cvolume_reset(volume
, ss
->channels
);
265 volume
->values
[0] = ((vol
& 0xFF) * PA_VOLUME_NORM
) / 100;
267 if (volume
->channels
>= 2)
268 volume
->values
[1] = (((vol
>> 8) & 0xFF) * PA_VOLUME_NORM
) / 100;
270 pa_log_debug("Read mixer settings: %s", pa_cvolume_snprint(cv
, sizeof(cv
), volume
));
274 int pa_oss_set_volume(int fd
, long mixer
, const pa_sample_spec
*ss
, const pa_cvolume
*volume
) {
275 char cv
[PA_CVOLUME_SNPRINT_MAX
];
279 l
= volume
->values
[0] > PA_VOLUME_NORM
? PA_VOLUME_NORM
: volume
->values
[0];
281 vol
= (l
*100)/PA_VOLUME_NORM
;
283 if (ss
->channels
>= 2) {
284 r
= volume
->values
[1] > PA_VOLUME_NORM
? PA_VOLUME_NORM
: volume
->values
[1];
285 vol
|= ((r
*100)/PA_VOLUME_NORM
) << 8;
288 if (ioctl(fd
, mixer
, &vol
) < 0)
291 pa_log_debug("Wrote mixer settings: %s", pa_cvolume_snprint(cv
, sizeof(cv
), volume
));
295 static int get_device_number(const char *dev
) {
299 if (readlink(dev
, buf
, sizeof(buf
)) < 0) {
300 if (errno
!= EINVAL
&& errno
!= ENOLINK
)
307 if ((e
= strrchr(p
, '/')))
315 if (*p
>= '0' && *p
<= '9')
321 int pa_oss_get_hw_description(const char *dev
, char *name
, size_t l
) {
326 if ((n
= get_device_number(dev
)) < 0)
329 if (!(f
= fopen("/dev/sndstat", "r")) &&
330 !(f
= fopen("/proc/sndstat", "r")) &&
331 !(f
= fopen("/proc/asound/oss/sndstat", "r"))) {
334 pa_log_warn("failed to open OSS sndstat device: %s", pa_cstrerror(errno
));
343 if (!fgets(line
, sizeof(line
), f
))
346 line
[strcspn(line
, "\r\n")] = 0;
349 b
= strcmp(line
, "Audio devices:") == 0;
356 if (sscanf(line
, "%i: ", &device
) != 1)
360 char *k
= strchr(line
, ':');
365 if (pa_endswith(k
, " (DUPLEX)"))
368 pa_strlcpy(name
, k
, l
);
378 static int open_mixer(const char *mixer
) {
381 if ((fd
= open(mixer
, O_RDWR
|O_NDELAY
|O_NOCTTY
)) >= 0)
387 int pa_oss_open_mixer_for_device(const char *device
) {
392 if ((n
= get_device_number(device
)) < 0)
396 if ((fd
= open_mixer("/dev/mixer")) >= 0)
399 fn
= pa_sprintf_malloc("/dev/mixer%i", n
);
404 pa_log_warn("Failed to open mixer '%s': %s", device
, pa_cstrerror(errno
));