2c573b2f5f18f06903dc391a58805f79f9f37776
[profile/ivi/pulseaudio.git] / src / modules / oss-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio 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.
10  
11   polypaudio 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.
15  
16   You should have received a copy of the GNU Lesser General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include <polypcore/util.h>
38 #include <polypcore/log.h>
39
40 #include "oss-util.h"
41
42 int pa_oss_open(const char *device, int *mode, int* pcaps) {
43     int fd = -1;
44     assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
45
46     if (*mode == O_RDWR) {
47         if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
48             int dcaps, *tcaps;
49             ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
50
51             tcaps = pcaps ? pcaps : &dcaps;
52             
53             if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
54                 pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", strerror(errno));
55                 goto fail;
56             }
57
58             if (*tcaps & DSP_CAP_DUPLEX)
59                 goto success;
60
61             pa_log_warn(__FILE__": '%s' doesn't support full duplex", device);
62
63             close(fd);
64         }
65         
66         if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
67             if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
68                 pa_log(__FILE__": open('%s'): %s", device, strerror(errno));
69                 goto fail;
70             }
71         }
72     } else {
73         if ((fd = open(device, *mode|O_NDELAY)) < 0) {
74             pa_log(__FILE__": open('%s'): %s", device, strerror(errno));
75             goto fail;
76         }
77     } 
78
79 success:
80
81     if (pcaps) {
82         if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
83             pa_log(__FILE__": SNDCTL_DSP_GETCAPS: %s", strerror(errno));
84             goto fail;
85         }
86     }
87
88     pa_fd_set_cloexec(fd, 1);
89     
90     return fd;
91
92 fail:
93     if (fd >= 0)
94         close(fd);
95     return -1;
96 }
97
98 int pa_oss_auto_format(int fd, pa_sample_spec *ss) {
99     int format, channels, speed, reqformat;
100     static const int format_trans[PA_SAMPLE_MAX] = {
101         [PA_SAMPLE_U8] = AFMT_U8,
102         [PA_SAMPLE_ALAW] = AFMT_A_LAW,
103         [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
104         [PA_SAMPLE_S16LE] = AFMT_S16_LE,
105         [PA_SAMPLE_S16BE] = AFMT_S16_BE,
106         [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
107         [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
108     };
109
110     assert(fd >= 0 && ss);
111
112     reqformat = format = format_trans[ss->format];
113     if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
114         format = AFMT_S16_NE;
115         if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
116             int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
117             format = f;
118             if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
119                 format = AFMT_U8;
120                 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
121                     pa_log(__FILE__": SNDCTL_DSP_SETFMT: %s", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
122                     return -1;
123                 } else
124                     ss->format = PA_SAMPLE_U8;
125             } else
126                 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
127         } else
128             ss->format = PA_SAMPLE_S16NE;
129     }
130         
131     channels = ss->channels;
132     if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
133         pa_log(__FILE__": SNDCTL_DSP_CHANNELS: %s", strerror(errno));
134         return -1;
135     }
136     assert(channels);
137     ss->channels = channels;
138
139     speed = ss->rate;
140     if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
141         pa_log(__FILE__": SNDCTL_DSP_SPEED: %s", strerror(errno));
142         return -1;
143     }
144     assert(speed);
145     ss->rate = speed;
146
147     return 0;
148 }
149
150 static int simple_log2(int v) {
151     int k = 0;
152
153     for (;;) {
154         v >>= 1;
155         if (!v) break;
156         k++;
157     }
158     
159     return k;
160 }
161
162 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
163     int arg;
164     arg = ((int) nfrags << 16) | simple_log2(frag_size);
165     
166     if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
167         pa_log(__FILE__": SNDCTL_DSP_SETFRAGMENT: %s", strerror(errno));
168         return -1;
169     }
170
171     return 0;
172 }
173
174 static int pa_oss_get_volume(int fd, int mixer, const pa_sample_spec *ss, pa_cvolume *volume) {
175     char cv[PA_CVOLUME_SNPRINT_MAX];
176     unsigned vol;
177
178     assert(fd >= 0);
179     assert(ss);
180     assert(volume);
181     
182     if (ioctl(fd, mixer, &vol) < 0)
183         return -1;
184
185     volume->values[0] = ((vol & 0xFF) * PA_VOLUME_NORM) / 100;
186
187     if ((volume->channels = ss->channels) >= 2)
188         volume->values[1] = (((vol >> 8) & 0xFF) * PA_VOLUME_NORM) / 100;
189
190     pa_log_debug(__FILE__": Read mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
191     return 0;
192 }
193
194 static int pa_oss_set_volume(int fd, int mixer, const pa_sample_spec *ss, const pa_cvolume *volume) {
195     char cv[PA_CVOLUME_SNPRINT_MAX];
196     unsigned vol;
197     pa_volume_t l, r;
198
199     l = volume->values[0] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[0];
200
201     vol = (l*100)/PA_VOLUME_NORM;
202
203     if (ss->channels >= 2) {
204         r = volume->values[1] > PA_VOLUME_NORM ? PA_VOLUME_NORM : volume->values[1];
205         vol |= ((r*100)/PA_VOLUME_NORM) << 8;
206     }
207     
208     if (ioctl(fd, mixer, &vol) < 0)
209         return -1;
210
211     pa_log_debug(__FILE__": Wrote mixer settings: %s", pa_cvolume_snprint(cv, sizeof(cv), volume));
212     return 0;
213 }
214
215 int pa_oss_get_pcm_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
216     return pa_oss_get_volume(fd, SOUND_MIXER_READ_PCM, ss, volume);
217 }
218
219 int pa_oss_set_pcm_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
220     return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_PCM, ss, volume);
221 }
222
223 int pa_oss_get_input_volume(int fd, const pa_sample_spec *ss, pa_cvolume *volume) {
224     return pa_oss_get_volume(fd, SOUND_MIXER_READ_IGAIN, ss, volume);
225 }
226
227 int pa_oss_set_input_volume(int fd, const pa_sample_spec *ss, const pa_cvolume *volume) {
228     return pa_oss_set_volume(fd, SOUND_MIXER_WRITE_IGAIN, ss, volume);
229 }
230
231 int pa_oss_get_hw_description(const char *dev, char *name, size_t l) {
232     FILE *f;
233     const char *e = NULL;
234     int n, r = -1;
235     int b = 0;
236
237     if (strncmp(dev, "/dev/dsp", 8) == 0)
238         e = dev+8;
239     else if (strncmp(dev, "/dev/adsp", 9) == 0)
240         e = dev+9;
241     else
242         return -1;
243
244     if (*e == 0)
245         n = 0;
246     else if (*e >= '0' && *e <= '9' && *(e+1) == 0)
247         n = *e - '0';
248     else
249         return -1;
250     
251     if (!(f = fopen("/dev/sndstat", "r")) &&
252         !(f = fopen("/proc/sndstat", "r")) &&
253         !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
254
255         if (errno != ENOENT)
256             pa_log_warn(__FILE__": failed to open OSS sndstat device: %s", strerror(errno));
257
258         return -1;
259     }
260
261     while (!feof(f)) {
262         char line[64];
263         int device;
264     
265         if (!fgets(line, sizeof(line), f))
266             break;
267
268         line[strcspn(line, "\r\n")] = 0;
269
270         if (!b) {
271             b = strcmp(line, "Audio devices:") == 0;
272             continue;
273         }
274
275         if (line[0] == 0)
276             break;
277         
278         if (sscanf(line, "%i: ", &device) != 1)
279             continue;
280
281         if (device == n) {
282             char *k = strchr(line, ':');
283             assert(k);
284             k++;
285             k += strspn(k, " ");
286
287             if (pa_endswith(k, " (DUPLEX)"))
288                 k[strlen(k)-9] = 0;
289             
290             pa_strlcpy(name, k, l);
291             r = 0;
292             break;
293         }
294     }
295
296     fclose(f);
297     return r;
298 }