aboutsummaryrefslogtreecommitdiffstats
path: root/sound/core
diff options
context:
space:
mode:
authorTakashi Iwai <tiwai@suse.de>2012-09-15 10:32:31 -0400
committerTakashi Iwai <tiwai@suse.de>2012-09-15 10:32:31 -0400
commite5d6db8e6039d7f51a9a648d74db1109c7fdc746 (patch)
tree1f8ec13b6bda70fdf6c607a86d02c3e974e4c469 /sound/core
parent62cbde1868b16e7cf1ed115cdfb9cbe82e230f0a (diff)
parent5efbc2610a7b2aac6c51f8fc943c019106568939 (diff)
Merge branch 'topic/tlv-chmap' into for-next
This is a merge of a topic branch containing the support for the new channel map API using control elements.
Diffstat (limited to 'sound/core')
-rw-r--r--sound/core/pcm.c4
-rw-r--r--sound/core/pcm_lib.c214
2 files changed, 218 insertions, 0 deletions
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 1a3070b4e5b5..f2991940b271 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -1105,6 +1105,10 @@ static int snd_pcm_dev_disconnect(struct snd_device *device)
1105 break; 1105 break;
1106 } 1106 }
1107 snd_unregister_device(devtype, pcm->card, pcm->device); 1107 snd_unregister_device(devtype, pcm->card, pcm->device);
1108 if (pcm->streams[cidx].chmap_kctl) {
1109 snd_ctl_remove(pcm->card, pcm->streams[cidx].chmap_kctl);
1110 pcm->streams[cidx].chmap_kctl = NULL;
1111 }
1108 } 1112 }
1109 unlock: 1113 unlock:
1110 mutex_unlock(&register_mutex); 1114 mutex_unlock(&register_mutex);
diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c
index 7ae671923393..f42c10a43315 100644
--- a/sound/core/pcm_lib.c
+++ b/sound/core/pcm_lib.c
@@ -26,6 +26,7 @@
26#include <linux/export.h> 26#include <linux/export.h>
27#include <sound/core.h> 27#include <sound/core.h>
28#include <sound/control.h> 28#include <sound/control.h>
29#include <sound/tlv.h>
29#include <sound/info.h> 30#include <sound/info.h>
30#include <sound/pcm.h> 31#include <sound/pcm.h>
31#include <sound/pcm_params.h> 32#include <sound/pcm_params.h>
@@ -2302,3 +2303,216 @@ snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2302} 2303}
2303 2304
2304EXPORT_SYMBOL(snd_pcm_lib_readv); 2305EXPORT_SYMBOL(snd_pcm_lib_readv);
2306
2307/*
2308 * standard channel mapping helpers
2309 */
2310
2311/* default channel maps for multi-channel playbacks, up to 8 channels */
2312const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
2313 { .channels = 1,
2314 .map = { SNDRV_CHMAP_MONO } },
2315 { .channels = 2,
2316 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2317 { .channels = 4,
2318 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2319 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2320 { .channels = 6,
2321 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2322 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2323 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
2324 { .channels = 8,
2325 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2326 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2327 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2328 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2329 { }
2330};
2331EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
2332
2333/* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
2334const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
2335 { .channels = 1,
2336 .map = { SNDRV_CHMAP_MONO } },
2337 { .channels = 2,
2338 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
2339 { .channels = 4,
2340 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2341 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2342 { .channels = 6,
2343 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2344 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2345 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
2346 { .channels = 8,
2347 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
2348 SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
2349 SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
2350 SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
2351 { }
2352};
2353EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
2354
2355static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
2356{
2357 if (ch > info->max_channels)
2358 return false;
2359 return !info->channel_mask || (info->channel_mask & (1U << ch));
2360}
2361
2362static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2363 struct snd_ctl_elem_info *uinfo)
2364{
2365 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2366
2367 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2368 uinfo->count = 0;
2369 uinfo->count = info->max_channels;
2370 uinfo->value.integer.min = 0;
2371 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2372 return 0;
2373}
2374
2375/* get callback for channel map ctl element
2376 * stores the channel position firstly matching with the current channels
2377 */
2378static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2380{
2381 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2382 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2383 struct snd_pcm_substream *substream;
2384 const struct snd_pcm_chmap_elem *map;
2385
2386 if (snd_BUG_ON(!info->chmap))
2387 return -EINVAL;
2388 substream = snd_pcm_chmap_substream(info, idx);
2389 if (!substream)
2390 return -ENODEV;
2391 memset(ucontrol->value.integer.value, 0,
2392 sizeof(ucontrol->value.integer.value));
2393 if (!substream->runtime)
2394 return 0; /* no channels set */
2395 for (map = info->chmap; map->channels; map++) {
2396 int i;
2397 if (map->channels == substream->runtime->channels &&
2398 valid_chmap_channels(info, map->channels)) {
2399 for (i = 0; i < map->channels; i++)
2400 ucontrol->value.integer.value[i] = map->map[i];
2401 return 0;
2402 }
2403 }
2404 return -EINVAL;
2405}
2406
2407/* tlv callback for channel map ctl element
2408 * expands the pre-defined channel maps in a form of TLV
2409 */
2410static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2411 unsigned int size, unsigned int __user *tlv)
2412{
2413 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2414 const struct snd_pcm_chmap_elem *map;
2415 unsigned int __user *dst;
2416 int c, count = 0;
2417
2418 if (snd_BUG_ON(!info->chmap))
2419 return -EINVAL;
2420 if (size < 8)
2421 return -ENOMEM;
2422 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2423 return -EFAULT;
2424 size -= 8;
2425 dst = tlv + 2;
2426 for (map = info->chmap; map->channels; map++) {
2427 int chs_bytes = map->channels * 4;
2428 if (!valid_chmap_channels(info, map->channels))
2429 continue;
2430 if (size < 8)
2431 return -ENOMEM;
2432 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
2433 put_user(chs_bytes, dst + 1))
2434 return -EFAULT;
2435 dst += 2;
2436 size -= 8;
2437 count += 8;
2438 if (size < chs_bytes)
2439 return -ENOMEM;
2440 size -= chs_bytes;
2441 count += chs_bytes;
2442 for (c = 0; c < map->channels; c++) {
2443 if (put_user(map->map[c], dst))
2444 return -EFAULT;
2445 dst++;
2446 }
2447 }
2448 if (put_user(count, tlv + 1))
2449 return -EFAULT;
2450 return 0;
2451}
2452
2453static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
2454{
2455 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2456 info->pcm->streams[info->stream].chmap_kctl = NULL;
2457 kfree(info);
2458}
2459
2460/**
2461 * snd_pcm_add_chmap_ctls - create channel-mapping control elements
2462 * @pcm: the assigned PCM instance
2463 * @stream: stream direction
2464 * @chmap: channel map elements (for query)
2465 * @max_channels: the max number of channels for the stream
2466 * @private_value: the value passed to each kcontrol's private_value field
2467 * @info_ret: store struct snd_pcm_chmap instance if non-NULL
2468 *
2469 * Create channel-mapping control elements assigned to the given PCM stream(s).
2470 * Returns zero if succeed, or a negative error value.
2471 */
2472int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
2473 const struct snd_pcm_chmap_elem *chmap,
2474 int max_channels,
2475 unsigned long private_value,
2476 struct snd_pcm_chmap **info_ret)
2477{
2478 struct snd_pcm_chmap *info;
2479 struct snd_kcontrol_new knew = {
2480 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
2481 .access = SNDRV_CTL_ELEM_ACCESS_READ |
2482 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2483 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2484 .info = pcm_chmap_ctl_info,
2485 .get = pcm_chmap_ctl_get,
2486 .tlv.c = pcm_chmap_ctl_tlv,
2487 };
2488 int err;
2489
2490 info = kzalloc(sizeof(*info), GFP_KERNEL);
2491 if (!info)
2492 return -ENOMEM;
2493 info->pcm = pcm;
2494 info->stream = stream;
2495 info->chmap = chmap;
2496 info->max_channels = max_channels;
2497 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
2498 knew.name = "Playback Channel Map";
2499 else
2500 knew.name = "Capture Channel Map";
2501 knew.device = pcm->device;
2502 knew.count = pcm->streams[stream].substream_count;
2503 knew.private_value = private_value;
2504 info->kctl = snd_ctl_new1(&knew, info);
2505 if (!info->kctl) {
2506 kfree(info);
2507 return -ENOMEM;
2508 }
2509 info->kctl->private_free = pcm_chmap_ctl_private_free;
2510 err = snd_ctl_add(pcm->card, info->kctl);
2511 if (err < 0)
2512 return err;
2513 pcm->streams[stream].chmap_kctl = info->kctl;
2514 if (info_ret)
2515 *info_ret = info;
2516 return 0;
2517}
2518EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);