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