Skip to content

Commit 42750b0

Browse files
author
Jaroslav Kysela
committed
[ALSA] Control API - TLV implementation for additional information like dB scale
This patch implements a TLV mechanism to transfer an additional information like dB scale to the user space. The types might be extended in future. Acked-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Jaroslav Kysela <perex@suse.cz>
1 parent 3eeab61 commit 42750b0

5 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎include/sound/asound.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ struct snd_timer_tread {
688688
* *
689689
****************************************************************************/
690690

691-
#define SNDRV_CTL_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 3)
691+
#define SNDRV_CTL_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 4)
692692

693693
struct snd_ctl_card_info {
694694
int card; /* card number */
@@ -818,6 +818,12 @@ struct snd_ctl_elem_value {
818818
unsigned char reserved[128-sizeof(struct timespec)];
819819
};
820820

821+
struct snd_ctl_tlv {
822+
unsigned int numid; /* control element numeric identification */
823+
unsigned int length; /* in bytes aligned to 4 */
824+
unsigned int tlv[0]; /* first TLV */
825+
};
826+
821827
enum {
822828
SNDRV_CTL_IOCTL_PVERSION = _IOR('U', 0x00, int),
823829
SNDRV_CTL_IOCTL_CARD_INFO = _IOR('U', 0x01, struct snd_ctl_card_info),
@@ -831,6 +837,7 @@ enum {
831837
SNDRV_CTL_IOCTL_ELEM_ADD = _IOWR('U', 0x17, struct snd_ctl_elem_info),
832838
SNDRV_CTL_IOCTL_ELEM_REPLACE = _IOWR('U', 0x18, struct snd_ctl_elem_info),
833839
SNDRV_CTL_IOCTL_ELEM_REMOVE = _IOWR('U', 0x19, struct snd_ctl_elem_id),
840+
SNDRV_CTL_IOCTL_TLV_READ = _IOWR('U', 0x1a, struct snd_ctl_tlv),
834841
SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE = _IOWR('U', 0x20, int),
835842
SNDRV_CTL_IOCTL_HWDEP_INFO = _IOR('U', 0x21, struct snd_hwdep_info),
836843
SNDRV_CTL_IOCTL_PCM_NEXT_DEVICE = _IOR('U', 0x30, int),

‎include/sound/control.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct snd_kcontrol_new {
4242
snd_kcontrol_info_t *info;
4343
snd_kcontrol_get_t *get;
4444
snd_kcontrol_put_t *put;
45+
unsigned int *tlv;
4546
unsigned long private_value;
4647
};
4748

@@ -58,6 +59,7 @@ struct snd_kcontrol {
5859
snd_kcontrol_info_t *info;
5960
snd_kcontrol_get_t *get;
6061
snd_kcontrol_put_t *put;
62+
unsigned int *tlv;
6163
unsigned long private_value;
6264
void *private_data;
6365
void (*private_free)(struct snd_kcontrol *kcontrol);

‎include/sound/tlv.h‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef __SOUND_TLV_H
2+
#define __SOUND_TLV_H
3+
4+
/*
5+
* Advanced Linux Sound Architecture - ALSA - Driver
6+
* Copyright (c) 2006 by Jaroslav Kysela <perex@suse.cz>
7+
*
8+
*
9+
* This program is free software; you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation; either version 2 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program; if not, write to the Free Software
21+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22+
*
23+
*/
24+
25+
/*
26+
* TLV structure is right behind the struct snd_ctl_tlv:
27+
* unsigned int type - see SNDRV_CTL_TLVT_*
28+
* unsigned int length
29+
* .... data aligned to sizeof(unsigned int), use
30+
* block_length = (length + (sizeof(unsigned int) - 1)) &
31+
* ~(sizeof(unsigned int) - 1)) ....
32+
*/
33+
34+
#define SNDRV_CTL_TLVT_CONTAINER 0 /* one level down - group of TLVs */
35+
#define SNDRV_CTL_TLVT_DB_SCALE 1 /* dB scale */
36+
37+
#define DECLARE_TLV_DB_SCALE(name, min, step, mute) \
38+
unsigned int name[] = { \
39+
SNDRV_CTL_TLVT_DB_SCALE, 2 * sizeof(unsigned int), \
40+
(min), ((step) & 0xffff) | ((mute) ? 0x10000 : 0) \
41+
}
42+
43+
#endif /* __SOUND_TLV_H */

‎sound/core/control.c‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol,
241241
kctl.info = ncontrol->info;
242242
kctl.get = ncontrol->get;
243243
kctl.put = ncontrol->put;
244+
kctl.tlv = ncontrol->tlv;
244245
kctl.private_value = ncontrol->private_value;
245246
kctl.private_data = private_data;
246247
return snd_ctl_new(&kctl, access);
@@ -1067,6 +1068,40 @@ static int snd_ctl_subscribe_events(struct snd_ctl_file *file, int __user *ptr)
10671068
return 0;
10681069
}
10691070

1071+
static int snd_ctl_tlv_read(struct snd_card *card,
1072+
struct snd_ctl_tlv __user *_tlv)
1073+
{
1074+
struct snd_ctl_tlv tlv;
1075+
struct snd_kcontrol *kctl;
1076+
unsigned int len;
1077+
int err = 0;
1078+
1079+
if (copy_from_user(&tlv, _tlv, sizeof(tlv)))
1080+
return -EFAULT;
1081+
if (tlv.length < sizeof(unsigned int) * 3)
1082+
return -EINVAL;
1083+
down_read(&card->controls_rwsem);
1084+
kctl = snd_ctl_find_numid(card, tlv.numid);
1085+
if (kctl == NULL) {
1086+
err = -ENOENT;
1087+
goto __kctl_end;
1088+
}
1089+
if (kctl->tlv == NULL) {
1090+
err = -ENXIO;
1091+
goto __kctl_end;
1092+
}
1093+
len = kctl->tlv[1] + 2 * sizeof(unsigned int);
1094+
if (tlv.length < len) {
1095+
err = -ENOMEM;
1096+
goto __kctl_end;
1097+
}
1098+
if (copy_to_user(_tlv->tlv, kctl->tlv, len))
1099+
err = -EFAULT;
1100+
__kctl_end:
1101+
up_read(&card->controls_rwsem);
1102+
return err;
1103+
}
1104+
10701105
static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
10711106
{
10721107
struct snd_ctl_file *ctl;
@@ -1086,11 +1121,11 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
10861121
case SNDRV_CTL_IOCTL_CARD_INFO:
10871122
return snd_ctl_card_info(card, ctl, cmd, argp);
10881123
case SNDRV_CTL_IOCTL_ELEM_LIST:
1089-
return snd_ctl_elem_list(ctl->card, argp);
1124+
return snd_ctl_elem_list(card, argp);
10901125
case SNDRV_CTL_IOCTL_ELEM_INFO:
10911126
return snd_ctl_elem_info_user(ctl, argp);
10921127
case SNDRV_CTL_IOCTL_ELEM_READ:
1093-
return snd_ctl_elem_read_user(ctl->card, argp);
1128+
return snd_ctl_elem_read_user(card, argp);
10941129
case SNDRV_CTL_IOCTL_ELEM_WRITE:
10951130
return snd_ctl_elem_write_user(ctl, argp);
10961131
case SNDRV_CTL_IOCTL_ELEM_LOCK:
@@ -1105,6 +1140,8 @@ static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg
11051140
return snd_ctl_elem_remove(ctl, argp);
11061141
case SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS:
11071142
return snd_ctl_subscribe_events(ctl, ip);
1143+
case SNDRV_CTL_IOCTL_TLV_READ:
1144+
return snd_ctl_tlv_read(card, argp);
11081145
case SNDRV_CTL_IOCTL_POWER:
11091146
return -ENOPROTOOPT;
11101147
case SNDRV_CTL_IOCTL_POWER_STATE:

‎sound/pci/ca0106/ca0106_mixer.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@
7070
#include <sound/pcm.h>
7171
#include <sound/ac97_codec.h>
7272
#include <sound/info.h>
73+
#include <sound/tlv.h>
7374

7475
#include "ca0106.h"
7576

77+
static DECLARE_TLV_DB_SCALE(snd_ca0106_db_scale, -5150, 75, 1);
78+
7679
static int snd_ca0106_shared_spdif_info(struct snd_kcontrol *kcontrol,
7780
struct snd_ctl_elem_info *uinfo)
7881
{
@@ -472,6 +475,7 @@ static int snd_ca0106_i2c_volume_put(struct snd_kcontrol *kcontrol,
472475
.info = snd_ca0106_volume_info, \
473476
.get = snd_ca0106_volume_get, \
474477
.put = snd_ca0106_volume_put, \
478+
.tlv = snd_ca0106_db_scale, \
475479
.private_value = ((chid) << 8) | (reg) \
476480
}
477481

0 commit comments

Comments
 (0)