Debian Codegraph @

oss/ctl_oss.c

raw symbols SCIP
1 /*
2 * ALSA <-> OSS mixer control plugin
3 *
4 * Copyright (c) 2005 by Takashi Iwai <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * TODO: implement the pseudo poll with thread (and pipe as pollfd)?
23 */
24
25 #include "config.h"
26 #include <stdio.h>
27 #include <sys/ioctl.h>
28 #include <alsa/asoundlib.h>
29 #include <alsa/control_external.h>
30 #if defined(__linux__)
31 #include <linux/soundcard.h>
32 #elif HAVE_SYS_SOUNDCARD_H
33 #include <sys/soundcard.h>
34 #elif HAVE_SOUNDCARD_H
35 #include <soundcard.h>
36 #endif
37
38 typedef struct snd_ctl_oss {
39 snd_ctl_ext_t ext;
40 char *device;
41 int fd;
42 int exclusive_input;
43 int stereo_mask;
44 unsigned int num_vol_ctls;
45 unsigned int vol_ctl[SOUND_MIXER_NRDEVICES];
46 unsigned int num_rec_items;
47 unsigned int rec_item[SOUND_MIXER_NRDEVICES];
48 } snd_ctl_oss_t;
49
50 static const char *const vol_devices[SOUND_MIXER_NRDEVICES] = {
51 [SOUND_MIXER_VOLUME] = "Master Playback Volume",
52 [SOUND_MIXER_BASS] = "Tone Control - Bass",
53 [SOUND_MIXER_TREBLE] = "Tone Control - Treble",
54 [SOUND_MIXER_SYNTH] = "Synth Playback Volume",
55 [SOUND_MIXER_PCM] = "PCM Playback Volume",
56 [SOUND_MIXER_SPEAKER] = "PC Speaker Playback Volume",
57 [SOUND_MIXER_LINE] = "Line Playback Volume",
58 [SOUND_MIXER_MIC] = "Mic Playback Volume",
59 [SOUND_MIXER_CD] = "CD Playback Volume",
60 [SOUND_MIXER_IMIX] = "Monitor Mix Playback Volume",
61 [SOUND_MIXER_ALTPCM] = "Headphone Playback Volume",
62 [SOUND_MIXER_RECLEV] = "Capture Volume",
63 [SOUND_MIXER_IGAIN] = "Capture Volume",
64 [SOUND_MIXER_OGAIN] = "Playback Volume",
65 [SOUND_MIXER_LINE1] = "Aux Playback Volume",
66 [SOUND_MIXER_LINE2] = "Aux1 Playback Volume",
67 [SOUND_MIXER_LINE3] = "Line1 Playback Volume",
68 [SOUND_MIXER_DIGITAL1] = "IEC958 Playback Volume",
69 [SOUND_MIXER_DIGITAL2] = "Digital Playback Volume",
70 [SOUND_MIXER_DIGITAL3] = "Digital1 Playback Volume",
71 [SOUND_MIXER_PHONEIN] = "Phone Playback Volume",
72 [SOUND_MIXER_PHONEOUT] = "Master Mono Playback Volume",
73 [SOUND_MIXER_VIDEO] = "Video Playback Volume",
74 [SOUND_MIXER_RADIO] = "Radio Playback Volume",
75 [SOUND_MIXER_MONITOR] = "Monitor Playback Volume",
76 };
77
78 static const char *const rec_devices[SOUND_MIXER_NRDEVICES] = {
79 [SOUND_MIXER_VOLUME] = "Mix Capture Switch",
80 [SOUND_MIXER_SYNTH] = "Synth Capture Switch",
81 [SOUND_MIXER_PCM] = "PCM Capture Switch",
82 [SOUND_MIXER_LINE] = "Line Capture Switch",
83 [SOUND_MIXER_MIC] = "Mic Capture Switch",
84 [SOUND_MIXER_CD] = "CD Capture Switch",
85 [SOUND_MIXER_LINE1] = "Aux Capture Switch",
86 [SOUND_MIXER_LINE2] = "Aux1 Capture Switch",
87 [SOUND_MIXER_LINE3] = "Line1 Capture Switch",
88 [SOUND_MIXER_DIGITAL1] = "IEC958 Capture Switch",
89 [SOUND_MIXER_DIGITAL2] = "Digital Capture Switch",
90 [SOUND_MIXER_DIGITAL3] = "Digital1 Capture Switch",
91 [SOUND_MIXER_PHONEIN] = "Phone Capture Switch",
92 [SOUND_MIXER_VIDEO] = "Video Capture Switch",
93 [SOUND_MIXER_RADIO] = "Radio Capture Switch",
94 };
95
96 static const char *const rec_items[SOUND_MIXER_NRDEVICES] = {
97 [SOUND_MIXER_VOLUME] = "Mix",
98 [SOUND_MIXER_SYNTH] = "Synth",
99 [SOUND_MIXER_PCM] = "PCM",
100 [SOUND_MIXER_LINE] = "Line",
101 [SOUND_MIXER_MIC] = "Mic",
102 [SOUND_MIXER_CD] = "CD",
103 [SOUND_MIXER_LINE1] = "Aux",
104 [SOUND_MIXER_LINE2] = "Aux1",
105 [SOUND_MIXER_LINE3] = "Line1",
106 [SOUND_MIXER_DIGITAL1] = "IEC958",
107 [SOUND_MIXER_DIGITAL2] = "Digital",
108 [SOUND_MIXER_DIGITAL3] = "Digital1",
109 [SOUND_MIXER_PHONEIN] = "Phone",
110 [SOUND_MIXER_VIDEO] = "Video",
111 [SOUND_MIXER_RADIO] = "Radio",
112 };
113
114 static void oss_close(snd_ctl_ext_t *ext)
115 {
116 snd_ctl_oss_t *oss = ext->private_data;
117
118 close(oss->fd);
119 free(oss->device);
120 free(oss);
121 }
122
123 static int oss_elem_count(snd_ctl_ext_t *ext)
124 {
125 snd_ctl_oss_t *oss = ext->private_data;
126 int num;
127
128 num = oss->num_vol_ctls;
129 if (oss->exclusive_input)
130 num++;
131 else if (oss->num_rec_items)
132 num += oss->num_rec_items;
133 return num;
134 }
135
136 static int oss_elem_list(snd_ctl_ext_t *ext, unsigned int offset, snd_ctl_elem_id_t *id)
137 {
138 snd_ctl_oss_t *oss = ext->private_data;
139
140 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
141 if (offset < oss->num_vol_ctls)
142 snd_ctl_elem_id_set_name(id, vol_devices[oss->vol_ctl[offset]]);
143 else if (oss->exclusive_input)
144 snd_ctl_elem_id_set_name(id, "Capture Source");
145 else {
146 offset -= oss->num_vol_ctls;
147 snd_ctl_elem_id_set_name(id, rec_devices[oss->rec_item[offset]]);
148 }
149 return 0;
150 }
151
152 #define OSS_KEY_DEVICE_MASK 0x1f
153 #define OSS_KEY_CAPTURE_FLAG (1 << 8)
154 #define OSS_KEY_CAPTURE_MUX (1 << 16)
155
156 static snd_ctl_ext_key_t oss_find_elem(snd_ctl_ext_t *ext,
157 const snd_ctl_elem_id_t *id)
158 {
159 snd_ctl_oss_t *oss = ext->private_data;
160 const char *name;
161 unsigned int i, key, numid;
162
163 numid = snd_ctl_elem_id_get_numid(id);
164 if (numid > 0) {
165 numid--;
166 if (numid < oss->num_vol_ctls)
167 return oss->vol_ctl[numid];
168 numid -= oss->num_vol_ctls;
169 if (oss->exclusive_input) {
170 if (!numid)
171 return OSS_KEY_CAPTURE_MUX;
172 } else if (numid < oss->num_rec_items)
173 return oss->rec_item[numid] |
174 OSS_KEY_CAPTURE_FLAG;
175 }
176
177 name = snd_ctl_elem_id_get_name(id);
178 if (! strcmp(name, "Capture Source")) {
179 if (oss->exclusive_input)
180 return OSS_KEY_CAPTURE_MUX;
181 else
182 return SND_CTL_EXT_KEY_NOT_FOUND;
183 }
184 for (i = 0; i < oss->num_vol_ctls; i++) {
185 key = oss->vol_ctl[i];
186 if (! strcmp(name, vol_devices[key]))
187 return key;
188 }
189 for (i = 0; i < oss->num_rec_items; i++) {
190 key = oss->rec_item[i];
191 if (! strcmp(name, rec_devices[key]))
192 return key | OSS_KEY_CAPTURE_FLAG;
193 }
194 return SND_CTL_EXT_KEY_NOT_FOUND;
195 }
196
197 static int oss_get_attribute(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key,
198 int *type, unsigned int *acc, unsigned int *count)
199 {
200 snd_ctl_oss_t *oss = ext->private_data;
201
202 if (key == OSS_KEY_CAPTURE_MUX) {
203 *type = SND_CTL_ELEM_TYPE_ENUMERATED;
204 *acc = SND_CTL_EXT_ACCESS_READWRITE;
205 *count = 1;
206 } else if (key & OSS_KEY_CAPTURE_FLAG) {
207 *type = SND_CTL_ELEM_TYPE_BOOLEAN;
208 *acc = SND_CTL_EXT_ACCESS_READWRITE;
209 *count = 1;
210 } else {
211 *type = SND_CTL_ELEM_TYPE_INTEGER;
212 *acc = SND_CTL_EXT_ACCESS_READWRITE;
213 if (oss->stereo_mask & (1 << key))
214 *count = 2;
215 else
216 *count = 1;
217 }
218 return 0;
219 }
220
221 static int oss_get_integer_info(snd_ctl_ext_t *ext ATTRIBUTE_UNUSED,
222 snd_ctl_ext_key_t key ATTRIBUTE_UNUSED,
223 long *imin, long *imax, long *istep)
224 {
225 *istep = 0;
226 *imin = 0;
227 *imax = 100;
228 return 0;
229 }
230
231 static int oss_get_enumerated_info(snd_ctl_ext_t *ext,
232 snd_ctl_ext_key_t key ATTRIBUTE_UNUSED,
233 unsigned int *items)
234 {
235 snd_ctl_oss_t *oss = ext->private_data;
236
237 *items = oss->num_rec_items;
238 return 0;
239 }
240
241 static int oss_get_enumerated_name(snd_ctl_ext_t *ext,
242 snd_ctl_ext_key_t key ATTRIBUTE_UNUSED,
243 unsigned int item, char *name,
244 size_t name_max_len)
245 {
246 snd_ctl_oss_t *oss = ext->private_data;
247
248 if (item >= oss->num_rec_items)
249 return -EINVAL;
250 item = oss->rec_item[item];
251 strncpy(name, rec_items[item], name_max_len - 1);
252 name[name_max_len - 1] = 0;
253 return 0;
254 }
255
256 static int oss_read_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value)
257 {
258 snd_ctl_oss_t *oss = ext->private_data;
259 int val;
260
261 if (key & OSS_KEY_CAPTURE_FLAG) {
262 key &= OSS_KEY_DEVICE_MASK;
263 if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &val) < 0)
264 return -errno;
265 *value = (val & (1 << key)) ? 1 : 0;
266 } else {
267 if (ioctl(oss->fd, MIXER_READ(key), &val) < 0)
268 return -errno;
269 *value = val & 0xff;
270 if (oss->stereo_mask & (1 << key))
271 value[1] = (val >> 8) & 0xff;
272 }
273 return 0;
274 }
275
276 static int oss_read_enumerated(snd_ctl_ext_t *ext,
277 snd_ctl_ext_key_t key ATTRIBUTE_UNUSED,
278 unsigned int *items)
279 {
280 snd_ctl_oss_t *oss = ext->private_data;
281 unsigned int i, val;
282
283 *items = 0;
284 if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &val) < 0)
285 return -errno;
286 for (i = 0; i < oss->num_rec_items; i++) {
287 if (val & (1 << oss->rec_item[i])) {
288 *items = i;
289 break;
290 }
291 }
292 return 0;
293 }
294
295 static int oss_write_integer(snd_ctl_ext_t *ext, snd_ctl_ext_key_t key, long *value)
296 {
297 snd_ctl_oss_t *oss = ext->private_data;
298 int val, oval;
299
300 if (key & OSS_KEY_CAPTURE_FLAG) {
301 key &= OSS_KEY_DEVICE_MASK;
302 if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &oval) < 0)
303 return -errno;
304 if (*value)
305 val = oval | (1 << key);
306 else
307 val = oval & ~(1 << key);
308 if (oval == val)
309 return 0;
310 if (ioctl(oss->fd, SOUND_MIXER_WRITE_RECSRC, &val) < 0)
311 return -errno;
312 return 1;
313 } else {
314 val = *value;
315 if (oss->stereo_mask & (1 << key))
316 val |= value[1] << 8;
317 if (ioctl(oss->fd, MIXER_READ(key), &oval) < 0)
318 return -errno;
319 if (oval == val)
320 return 0;
321 if (ioctl(oss->fd, MIXER_WRITE(key), &val) < 0)
322 return -errno;
323 return 1;
324 }
325 }
326
327 static int oss_write_enumerated(snd_ctl_ext_t *ext,
328 snd_ctl_ext_key_t key ATTRIBUTE_UNUSED,
329 unsigned int *items)
330 {
331 snd_ctl_oss_t *oss = ext->private_data;
332 int val, oval;
333
334 if (ioctl(oss->fd, SOUND_MIXER_READ_RECSRC, &oval) < 0)
335 return -errno;
336 val = 1 << oss->rec_item[*items];
337 if (val == oval)
338 return 0;
339 if (ioctl(oss->fd, SOUND_MIXER_WRITE_RECSRC, &val) < 0)
340 return -errno;
341 return 1;
342 }
343
344 static int oss_read_event(snd_ctl_ext_t *ext ATTRIBUTE_UNUSED,
345 snd_ctl_elem_id_t *id ATTRIBUTE_UNUSED,
346 unsigned int *event_mask ATTRIBUTE_UNUSED)
347 {
348 return -EAGAIN;
349 }
350
351 static snd_ctl_ext_callback_t oss_ext_callback = {
352 .close = oss_close,
353 .elem_count = oss_elem_count,
354 .elem_list = oss_elem_list,
355 .find_elem = oss_find_elem,
356 .get_attribute = oss_get_attribute,
357 .get_integer_info = oss_get_integer_info,
358 .get_enumerated_info = oss_get_enumerated_info,
359 .get_enumerated_name = oss_get_enumerated_name,
360 .read_integer = oss_read_integer,
361 .read_enumerated = oss_read_enumerated,
362 .write_integer = oss_write_integer,
363 .write_enumerated = oss_write_enumerated,
364 .read_event = oss_read_event,
365 };
366
367
368 SND_CTL_PLUGIN_DEFINE_FUNC(oss)
369 {
370 snd_config_iterator_t it, next;
371 const char *device = "/dev/mixer";
372 struct mixer_info mixinfo;
373 int i, err, val;
374 snd_ctl_oss_t *oss;
375
376 snd_config_for_each(it, next, conf) {
377 snd_config_t *n = snd_config_iterator_entry(it);
378 const char *id;
379 if (snd_config_get_id(n, &id) < 0)
380 continue;
381 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
382 continue;
383 if (strcmp(id, "device") == 0) {
384 if (snd_config_get_string(n, &device) < 0) {
385 SNDERR("Invalid type for %s", id);
386 return -EINVAL;
387 }
388 continue;
389 }
390 SNDERR("Unknown field %s", id);
391 return -EINVAL;
392 }
393
394 oss = calloc(1, sizeof(*oss));
395 oss->device = strdup(device);
396 oss->fd = -1;
397 if (oss->device == NULL) {
398 SNDERR("cannot allocate");
399 free(oss);
400 return -ENOMEM;
401 }
402 oss->fd = open(device, O_RDWR);
403 if (oss->fd < 0) {
404 err = -errno;
405 SNDERR("Cannot open device %s", device);
406 goto error;
407 }
408
409 if (ioctl(oss->fd, SOUND_MIXER_INFO, &mixinfo) < 0) {
410 err = -errno;
411 SNDERR("Cannot get mixer info for device %s", device);
412 goto error;
413 }
414
415 oss->ext.version = SND_CTL_EXT_VERSION;
416 oss->ext.card_idx = 0; /* FIXME */
417 strncpy(oss->ext.id, mixinfo.id, sizeof(oss->ext.id) - 1);
418 strcpy(oss->ext.driver, "OSS-Emulation");
419 strncpy(oss->ext.name, mixinfo.name, sizeof(oss->ext.name) - 1);
420 strncpy(oss->ext.longname, mixinfo.name, sizeof(oss->ext.longname) - 1);
421 strncpy(oss->ext.mixername, mixinfo.name, sizeof(oss->ext.mixername) - 1);
422 oss->ext.poll_fd = -1;
423 oss->ext.callback = &oss_ext_callback;
424 oss->ext.private_data = oss;
425
426 oss->num_vol_ctls = 0;
427 val = 0;
428 if (ioctl(oss->fd, SOUND_MIXER_READ_DEVMASK, &val) < 0)
429 perror("ctl_oss: DEVMASK error");
430 else {
431 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
432 if ((val & (1 << i)) && vol_devices[i])
433 oss->vol_ctl[oss->num_vol_ctls++] = i;
434 }
435 }
436
437 if (ioctl(oss->fd, SOUND_MIXER_READ_STEREODEVS, &oss->stereo_mask) < 0)
438 perror("ctl_oss: STEREODEVS error");
439 val = 0;
440 if (ioctl(oss->fd, SOUND_MIXER_READ_CAPS, &val) < 0)
441 perror("ctl_oss: MIXER_CAPS error");
442 else if (val & SOUND_CAP_EXCL_INPUT)
443 oss->exclusive_input = 1;
444
445 oss->num_rec_items = 0;
446 val = 0;
447 if (ioctl(oss->fd, SOUND_MIXER_READ_RECMASK, &val) < 0)
448 perror("ctl_oss: MIXER_RECMASK error");
449 else {
450 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
451 if (val & (1 << i)) {
452 if (oss->exclusive_input) {
453 if (! rec_items[i])
454 continue;
455 } else {
456 if (! rec_devices[i])
457 continue;
458 }
459 oss->rec_item[oss->num_rec_items++] = i;
460 }
461 }
462 }
463 if (! oss->num_rec_items)
464 oss->exclusive_input = 0;
465
466 err = snd_ctl_ext_create(&oss->ext, name, mode);
467 if (err < 0)
468 goto error;
469
470 *handlep = oss->ext.handle;
471 return 0;
472
473 error:
474 if (oss->fd >= 0)
475 close(oss->fd);
476 free(oss->device);
477 free(oss);
478 return err;
479 }
480
481 SND_CTL_PLUGIN_SYMBOL(oss);