4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
33 #include <sys/types.h>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
43 int pa_oss_open(const char *device
, int *mode
, int* pcaps
) {
47 assert(device
&& mode
&& (*mode
== O_RDWR
|| *mode
== O_RDONLY
|| *mode
== O_WRONLY
));
52 if (*mode
== O_RDWR
) {
53 if ((fd
= open(device
, O_RDWR
|O_NDELAY
)) >= 0) {
55 ioctl(fd
, SNDCTL_DSP_SETDUPLEX
, 0);
57 tcaps
= pcaps
? pcaps
: &dcaps
;
59 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, tcaps
) < 0) {
60 pa_log(__FILE__
": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno
));
64 if (*tcaps
& DSP_CAP_DUPLEX
)
67 pa_log_warn(__FILE__
": '%s' doesn't support full duplex", device
);
72 if ((fd
= open(device
, (*mode
= O_WRONLY
)|O_NDELAY
)) < 0) {
73 if ((fd
= open(device
, (*mode
= O_RDONLY
)|O_NDELAY
)) < 0) {
74 pa_log(__FILE__
": open('%s'): %s", device
, pa_cstrerror(errno
));
79 if ((fd
= open(device
, *mode
|O_NDELAY
)) < 0) {
80 pa_log(__FILE__
": open('%s'): %s", device
, pa_cstrerror(errno
));
89 if (ioctl(fd
, SNDCTL_DSP_GETCAPS
, pcaps
) < 0) {
90 pa_log(__FILE__
": SNDCTL_DSP_GETCAPS: %s", pa_cstrerror(errno
));
94 pa_log_debug(__FILE__
": capabilities:%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
95 *pcaps
& DSP_CAP_BATCH
? " BATCH" : "",
96 *pcaps
& DSP_CAP_BIND
? " BIND" : "",
97 *pcaps
& DSP_CAP_COPROC
? " COPROC" : "",
98 *pcaps
& DSP_CAP_DUPLEX
? " DUPLEX" : "",
99 #ifdef DSP_CAP_FREERATE
100 *pcaps
& DSP_CAP_FREERATE
? " FREERATE" : "",
105 *pcaps
& DSP_CAP_INPUT
? " INPUT" : "",
109 *pcaps
& DSP_CAP_MMAP
? " MMAP" : "",
111 *pcaps
& DSP_CAP_MODEM
? " MODEM" : "",
115 *pcaps
& DSP_CAP_MULTI
? " MULTI" : "",
116 #ifdef DSP_CAP_OUTPUT
117 *pcaps
& DSP_CAP_OUTPUT
? " OUTPUT" : "",
121 *pcaps
& DSP_CAP_REALTIME
? " REALTIME" : "",
122 #ifdef DSP_CAP_SHADOW
123 *pcaps
& DSP_CAP_SHADOW
? " SHADOW" : "",
127 #ifdef DSP_CAP_VIRTUAL
128 *pcaps
& DSP_CAP_VIRTUAL
? " VIRTUAL" : "",
132 *pcaps
& DSP_CAP_TRIGGER
? " TRIGGER" : "");
134 pa_fd_set_cloexec(fd
, 1);
144 int pa_oss_auto_format(int fd
, pa_sample_spec
*ss
) {
145 int format
, channels
, speed
, reqformat
;
146 pa_sample_format_t orig_format
;
148 static const int format_trans
[PA_SAMPLE_MAX
] = {
149 [PA_SAMPLE_U8
] = AFMT_U8
,
150 [PA_SAMPLE_ALAW
] = AFMT_A_LAW
,
151 [PA_SAMPLE_ULAW
] = AFMT_MU_LAW
,
152 [PA_SAMPLE_S16LE
] = AFMT_S16_LE
,
153 [PA_SAMPLE_S16BE
] = AFMT_S16_BE
,
154 [PA_SAMPLE_FLOAT32LE
] = AFMT_QUERY
, /* not supported */
155 [PA_SAMPLE_FLOAT32BE
] = AFMT_QUERY
, /* not supported */
158 assert(fd
>= 0 && ss
);
160 orig_format
= ss
->format
;
162 reqformat
= format
= format_trans
[ss
->format
];
163 if (reqformat
== AFMT_QUERY
|| ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= reqformat
) {
164 format
= AFMT_S16_NE
;
165 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= AFMT_S16_NE
) {
166 int f
= AFMT_S16_NE
== AFMT_S16_LE
? AFMT_S16_BE
: AFMT_S16_LE
;
168 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= f
) {
170 if (ioctl(fd
, SNDCTL_DSP_SETFMT
, &format
) < 0 || format
!= AFMT_U8
) {
171 pa_log(__FILE__
": SNDCTL_DSP_SETFMT: %s", format
!= AFMT_U8
? "No supported sample format" : pa_cstrerror(errno
));
174 ss
->format
= PA_SAMPLE_U8
;
176 ss
->format
= f
== AFMT_S16_LE
? PA_SAMPLE_S16LE
: PA_SAMPLE_S16BE
;
178 ss
->format
= PA_SAMPLE_S16NE
;
181 if (orig_format
!= ss
->format
)
182 pa_log_warn(__FILE__
": device doesn't support sample format %s, changed to %s.",
183 pa_sample_format_to_string(orig_format
),
184 pa_sample_format_to_string(ss
->format
));
186 channels
= ss
->channels
;
187 if (ioctl(fd
, SNDCTL_DSP_CHANNELS
, &channels
) < 0) {
188 pa_log(__FILE__
": SNDCTL_DSP_CHANNELS: %s", pa_cstrerror(errno
));
191 assert(channels
> 0);
193 if (ss
->channels
!= channels
) {
194 pa_log_warn(__FILE__
": device doesn't support %i channels, using %i channels.", ss
->channels
, channels
);
195 ss
->channels
= channels
;
199 if (ioctl(fd
, SNDCTL_DSP_SPEED
, &speed
) < 0) {
200 pa_log(__FILE__
": SNDCTL_DSP_SPEED: %s", pa_cstrerror(errno
));
205 if (ss
->rate
!= (unsigned) speed
) {
206 pa_log_warn(__FILE__
": device doesn't support %i Hz, changed to %i Hz.", ss
->rate
, speed
);
208 /* If the sample rate deviates too much, we need to resample */
209 if (speed
< ss
->rate
*.95 || speed
> ss
->rate
*1.05)
216 static int simple_log2(int v
) {
228 int pa_oss_set_fragments(int fd
, int nfrags
, int frag_size
) {
230 arg
= ((int) nfrags
<< 16) | simple_log2(frag_size
);
232 if (ioctl(fd
, SNDCTL_DSP_SETFRAGMENT
, &arg
) < 0) {
233 pa_log(__FILE__
": SNDCTL_DSP_SETFRAGMENT: %s", pa_cstrerror(errno
));
240 static int pa_oss_get_volume(int fd
, int mixer
, const pa_sample_spec
*ss
, pa_cvolume
*volume
) {
241 char cv
[PA_CVOLUME_SNPRINT_MAX
];
248 if (ioctl(fd
, mixer
, &vol
) < 0)
251 volume
->values
[0] = ((vol
& 0xFF) * PA_VOLUME_NORM
) / 100;
253 if ((volume
->channels
= ss
->channels
) >= 2)
254 volume
->values
[1] = (((vol
>> 8) & 0xFF) * PA_VOLUME_NORM
) / 100;
256 pa_log_debug(__FILE__
": Read mixer settings: %s", pa_cvolume_snprint(cv
, sizeof(cv
), volume
));
260 static int pa_oss_set_volume(int fd
, int mixer
, const pa_sample_spec
*ss
, const pa_cvolume
*volume
) {
261 char cv
[PA_CVOLUME_SNPRINT_MAX
];
265 l
= volume
->values
[0] > PA_VOLUME_NORM
? PA_VOLUME_NORM
: volume
->values
[0];
267 vol
= (l
*100)/PA_VOLUME_NORM
;
269 if (ss
->channels
>= 2) {
270 r
= volume
->values
[1] > PA_VOLUME_NORM
? PA_VOLUME_NORM
: volume
->values
[1];
271 vol
|= ((r
*100)/PA_VOLUME_NORM
) << 8;
274 if (ioctl(fd
, mixer
, &vol
) < 0)
277 pa_log_debug(__FILE__
": Wrote mixer settings: %s", pa_cvolume_snprint(cv
, sizeof(cv
), volume
));
281 int pa_oss_get_pcm_volume(int fd
, const pa_sample_spec
*ss
, pa_cvolume
*volume
) {
282 return pa_oss_get_volume(fd
, SOUND_MIXER_READ_PCM
, ss
, volume
);
285 int pa_oss_set_pcm_volume(int fd
, const pa_sample_spec
*ss
, const pa_cvolume
*volume
) {
286 return pa_oss_set_volume(fd
, SOUND_MIXER_WRITE_PCM
, ss
, volume
);
289 int pa_oss_get_input_volume(int fd
, const pa_sample_spec
*ss
, pa_cvolume
*volume
) {
290 return pa_oss_get_volume(fd
, SOUND_MIXER_READ_IGAIN
, ss
, volume
);
293 int pa_oss_set_input_volume(int fd
, const pa_sample_spec
*ss
, const pa_cvolume
*volume
) {
294 return pa_oss_set_volume(fd
, SOUND_MIXER_WRITE_IGAIN
, ss
, volume
);
297 int pa_oss_get_hw_description(const char *dev
, char *name
, size_t l
) {
299 const char *e
= NULL
;
303 if (strncmp(dev
, "/dev/dsp", 8) == 0)
305 else if (strncmp(dev
, "/dev/adsp", 9) == 0)
312 else if (*e
>= '0' && *e
<= '9' && *(e
+1) == 0)
317 if (!(f
= fopen("/dev/sndstat", "r")) &&
318 !(f
= fopen("/proc/sndstat", "r")) &&
319 !(f
= fopen("/proc/asound/oss/sndstat", "r"))) {
322 pa_log_warn(__FILE__
": failed to open OSS sndstat device: %s", pa_cstrerror(errno
));
331 if (!fgets(line
, sizeof(line
), f
))
334 line
[strcspn(line
, "\r\n")] = 0;
337 b
= strcmp(line
, "Audio devices:") == 0;
344 if (sscanf(line
, "%i: ", &device
) != 1)
348 char *k
= strchr(line
, ':');
353 if (pa_endswith(k
, " (DUPLEX)"))
356 pa_strlcpy(name
, k
, l
);