Linux ALSA驱动之 controls

8 阅读9分钟

ALSA 的控件机制是 ALSA 驱动框架中一个核心且强大的部分,它为用户空间程序(如混音器、音频服务器 PulseAudio/PipeWire、媒体播放器等)提供了一个统一、标准的接口,用于查询和操作声卡上各种复杂的音频参数。

1. tinymix

tinymix 是一个用于控制 Android 系统音频混音器(Audio Mixer)的命令行工具

1.1 使用范例

"DAC VOLUME" 是 ALSA 框架中 用于控制 DAC 声音输出音量的控件。

查询当前音量:

tinymix "DAC VOLUME"        

返回:

DAC VOLUME: 191 (dsrange 0->255)

设置音量:

tinymix "DAC VOLUME" 225

查询是否设置成功:

tinymix "DAC VOLUME"

返回:

DAC VOLUME: 225 (dsrange 0->255)

1.2 与内核交互的系统调用序列

# 用户命令
tinymix "DAC VOLUME" 225

# 实际系统调用序列
1. open("/dev/snd/controlC0", O_RDWR)        # 打开控制设备
2. ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ...) # 查询控件信息
3. ioctl(fd, SNDRV_CTL_IOCTL_ELEM_READ, ...) # 读取当前值
4. ioctl(fd, SNDRV_CTL_IOCTL_ELEM_WRITE, ...)# 写入新值
5. close(fd)

由此引出问题: 声卡控制设备节点 "/dev/snd/controlC0" 与控件 "DAC VOLUME" 在内核中是怎样建立联系的?

2. Kernel Space

先上个 "DAC VOLUME" 控件与声卡实例 snd_soc_card_holi_msm 之间的关联图:

card-control.png

2.1 Codec Driver

2.1.1 注册 snd_soc_component

es8311_i2c_probe()中:

static const struct snd_kcontrol_new es8311_snd_controls[] = {
	SOC_SINGLE_TLV("DAC VOLUME", ES8311_DAC_REG32,
			0, 255, 0, vdac_tlv),
}
static struct snd_soc_component_driver soc_component_dev_es8311 = {
	.controls = es8311_snd_controls,
	.num_controls = ARRAY_SIZE(es8311_snd_controls),
};

static int es8311_i2c_probe(struct i2c_client *i2c_client,
					const struct i2c_device_id *id)
{
	ret =  snd_soc_register_component(&i2c_client->dev,
			&soc_component_dev_es8311,
			&es8311_dai,
			1);
}

snd_soc_register_component()里会创建一个snd_soc_component实例,我们姑且命名为es8311_component:

//sound/soc/soc-core.c
int snd_soc_register_component(struct device *dev,
			const struct snd_soc_component_driver *component_driver,
			struct snd_soc_dai_driver *dai_drv,
			int num_dai)
{
	struct snd_soc_component *component;
	/* 创建 snd_soc_component 实例 */
	component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
	if (!component)
		return -ENOMEM;

	return snd_soc_add_component(dev, component, component_driver,
				     dai_drv, num_dai);
}

接着走

snd_soc_add_component()
├── snd_soc_component_initialize()
└── snd_soc_component_add()
  • snd_soc_component_initialize() 里将关系图中的es8311_devsoc_component_dev_es8311赋值给es8311_component
  • snd_soc_component_add()里将es8311_component链到全局变量component_list

至此,代表es8311snd_soc_component已注册完成,通过遍历全局变量component_list可找到es8311_component"DAC VOLUME"控件和es8311_component的关系如下:

es8311_component : snd_soc_component
└── soc_component_dev_es8311 : snd_soc_component_driver
    └── es8311_snd_controls[] : snd_kcontrol_new
        ├── "MIC PGA GAIN"
        ├── "DAC VOLUME"
        ├── ....
        └── "MCLK SOURCE"

2.2 Card Driver

2.2.1 注册 snd_soc_card

msm_asoc_machine_probe() {
    struct snd_soc_card card = populate_snd_card_dailinks(&pdev->dev)
    msm_populate_dai_link_component_of_node(card)
    devm_snd_soc_register_card(&pdev->dev, card)
}

msm_asoc_machine_probe():

  • snd_soc_card实例snd_soc_card_holi_msm填充dai_link,见populate_snd_card_dailinks()
  • dai_link填充of_node,见msm_populate_dai_link_component_of_node()
  • 注册snd_soc_card_holi_msm

2.2.2 绑定 snd_soc_dai_link

devm_snd_soc_register_card()
└── snd_soc_register_card()
    └── snd_soc_bind_card()
        └── snd_soc_instantiate_card()
            ├── soc_init_dai_link()
            |   └── soc_find_component()
            |       └── snd_soc_is_matching_component()
            └── soc_bind_dai_link()

soc_init_dai_link()里会判断snd_soc_dai_link.codecsnd_soc_dai_link.platform对应的snd_soc_component是否已经注册到全局变量component_list里:

static int soc_init_dai_link(struct snd_soc_card *card,
			     struct snd_soc_dai_link *link)
{
	for_each_link_codecs(link, i, codec) {
		/*
		 * Defer card registration if codec component is not added to
		 * component list.
		 */
		if (!soc_find_component(codec))
			return -EPROBE_DEFER;
	}
}
static struct snd_soc_component *soc_find_component(
	const struct snd_soc_dai_link_component *dlc)
{
	for_each_component(component)
		if (snd_soc_is_matching_component(dlc, component))
			return component;

	return NULL;
}

snd_soc_is_matching_component()的匹配方法中使用的of_nodemsm_populate_dai_link_component_of_node()中填充的of_node是相对应的:

static int snd_soc_is_matching_component(
	const struct snd_soc_dai_link_component *dlc,
	struct snd_soc_component *component)
{
	struct device_node *component_of_node;

	if (!dlc)
		return 0;

	component_of_node = soc_component_to_node(component);

	if (dlc->of_node && component_of_node != dlc->of_node)
		return 0;
	if (dlc->name && strcmp(component->name, dlc->name))
		return 0;

	return 1;
}

snd_soc_is_matching_component()示意图:

snd_soc_is_matching_component.png

soc_bind_dai_link()
|
|   /* 创建 snd_soc_pcm_runtime */
├── soc_new_pcm_runtime()
|
|   /* 查找匹配的 snd_soc_dai 绑定到 snd_soc_pcm_runtime */
├── snd_soc_find_dai()
|
|   /* 将上面 snd_soc_dai 对应的 snd_soc_component
|    绑定到 snd_soc_pcm_runtime*/
├── snd_soc_rtdcom_add()
|
|   /* 将 snd_soc_pcm_runtime 绑定到 snd_soc_card */
└── soc_add_pcm_runtime()

如何查找匹配的 snd_soc_dai: snd_soc_find_dai(const struct snd_soc_dai_link_component *dlc)

  • 遍历全局变量component_list中的snd_soc_component实例
  • snd_soc_component.dai_list中遍历出snd_soc_dai实例
  • snd_soc_dai.namesnd_soc_dai.driver.namesnd_soc_dai_link_component.dai_name相等的话,返回相应的snd_soc_dai

snd_soc_find_dai()函数示意图:

snd_soc_find_dai.png

snd_soc_rtdcom_add()snd_soc_dai.component指向的snd_soc_component加到snd_soc_pcm_runtime.component_list

2.2.3 创建snd_cardcontrolC0文件

snd_soc_instantiate_card()
|   /* 在 snd_soc_card 绑定 snd_soc_dai_link 完成后, 创建 snd_card */
└── snd_card_new() 
    └── snd_ctl_create() //创建 /dev/snd/controlC0 文件

struct snd_card用来关联controlssnd_soc_card:

snd_soc_card
└── snd_card
    └── controls[0..N] : snd_kcontrol   

/dev/snd/controlC0 是 ALSA 控制接口设备文件,主要用于音频系统的控制和状态管理:

a. 音频设备控制
  • 音量控制:调节主音量、PCM音量、各通道音量
  • 通道开关:静音/取消静音
  • 音效控制:均衡器、3D音效、重低音等
  • 路由切换:输入输出端口选择
b. 设备信息查询
  • 获取设备能力(支持的格式、采样率等)
  • 查询混音器(mixer)元素信息
  • 枚举可用音频设备
c. 音频路由管理
  • 控制音频数据流的路径
  • 管理多个音频源/目的地的连接 例如tinymix命令就是通过操作/dev/snd/controlC0 这个文件来控制音频系统的。

2.2.3 soc_probe_link_components()

soc_probe_link_components()用于 probe snd_soc_card 上所有被 snd_soc_dai_link 使用的 snd_soc_component

  • 将相关联的 N 个snd_soc_component加到snd_soc_card的管理链表里
  • 将相关联的 N 个snd_kcontrol_new加到snd_soc_card.snd_card的管理链表里
snd_soc_instantiate_card()
└── soc_probe_link_components()
static int soc_probe_link_components(struct snd_soc_card *card)
{
	struct snd_soc_component *component;
	struct snd_soc_pcm_runtime *rtd;
	struct snd_soc_rtdcom_list *rtdcom;
	int ret, order;
	dev_err(card->dev, "%s: %s\n", __func__, card->name);
	for_each_comp_order(order) {
		for_each_card_rtds(card, rtd) {
			for_each_rtdcom(rtd, rtdcom) {
				component = rtdcom->component;
				if (component->driver->probe_order != order)
					continue;
				ret = soc_probe_component(card, component);
				if (ret < 0)
					return ret;
			}
		}
	}
	return 0;
}

soc_probe_link_components(struct snd_soc_card *card)

  • snd_soc_card中遍历出snd_soc_pcm_runtime
  • snd_soc_pcm_runtime中遍历出snd_soc_component
  • 调用soc_probe_component(card, component)
soc_probe_link_components()
└── soc_probe_component(snd_soc_card *card, nd_soc_component *component)
    |
    ├── component->card = card;
    |
    ├── snd_soc_add_component_controls() 添加控制项
    |   |
    |   |   /* 将控制项添加到 snd_card 的 controls 链表中 */
    |   └── snd_soc_add_controls()
    |
    |   /* 将component添加到card的component_dev_list中 */
    └── list_add(&component->card_list, &card->component_dev_list);

snd_soc_add_controls()snd_kcontrol_new类型的controls转换为snd_kcontrol添加到snd_card.controls里。

3. tinymix -> CODEC芯片 数据流

读取control的值

int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
{
	struct snd_ctl_elem_value ev;
    memset(&ev, 0, sizeof(ev));
    ev.id.numid = ctl->info->id.numid;
    ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
}

kernelSNDRV_CTL_IOCTL_ELEM_READ命令的实现:

static long snd_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case SNDRV_CTL_IOCTL_ELEM_READ:
		return snd_ctl_elem_read_user(card, argp);
	}
}

static int snd_ctl_elem_read_user(struct snd_card *card,
				  struct snd_ctl_elem_value __user *_control)
{
	struct snd_ctl_elem_value *control;
	control = memdup_user(_control, sizeof(*control));
	result = snd_ctl_elem_read(card, control);
	if (copy_to_user(_control, control, sizeof(*control)))
		result = -EFAULT;
}

static int snd_ctl_elem_read(struct snd_card *card,
			     struct snd_ctl_elem_value *control)
{
	struct snd_kcontrol *kctl;
	struct snd_kcontrol_volatile *vd;
	unsigned int index_offset;

	kctl = snd_ctl_find_id(card, &control->id);
	if (kctl == NULL)
		return -ENOENT;

	index_offset = snd_ctl_get_ioff(kctl, &control->id);
	vd = &kctl->vd[index_offset];
	if (!(vd->access & SNDRV_CTL_ELEM_ACCESS_READ) || kctl->get == NULL)
		return -EPERM;

	snd_ctl_build_ioff(&control->id, kctl, index_offset);
	return kctl->get(kctl, control);
}

kctl->get指向哪里呢,这里插个"DAC VOLUME"的定义:

static const struct snd_kcontrol_new es8311_snd_controls[] = {
	SOC_SINGLE_TLV("DAC VOLUME", ES8311_DAC_REG32,
			0, 255, 0, vdac_tlv),
}

#define SOC_DOUBLE_VALUE(xreg, shift_left, shift_right, xmax, xinvert, xautodisable) \
	((unsigned long)&(struct soc_mixer_control) \
	{.reg = xreg, .rreg = xreg, .shift = shift_left, \
	.rshift = shift_right, .max = xmax, .platform_max = xmax, \
	.invert = xinvert, .autodisable = xautodisable})
#define SOC_SINGLE_VALUE(xreg, xshift, xmax, xinvert, xautodisable) \
	SOC_DOUBLE_VALUE(xreg, xshift, xshift, xmax, xinvert, xautodisable)

#define SOC_SINGLE_TLV(xname, reg, shift, max, invert, tlv_array) \
{	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
	.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
		 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
	.tlv.p = (tlv_array), \
	.info = snd_soc_info_volsw, .get = snd_soc_get_volsw,\
	.put = snd_soc_put_volsw, \
	.private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, 0) }

展开宏后:

{
    .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
    .name = "DAC VOLUME",
    .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | SNDRV_CTL_ELEM_ACCESS_READWRITE,
    .tlv.p = (vadc_tlv),
    .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,
    .put = snd_soc_put_volsw,
    .private_value = ((unsigned long)&(struct soc_mixer_control) 
         {.reg = ES8311_DAC_REG32, .rreg = ES8311_DAC_REG32, .shift = 0, \
	.rshift = 0, .max = 255, .platform_max = 255, \
	.invert = 0, .autodisable = 0})
}

所以,"DAC VOLUME"kctl->get 指向函数snd_soc_get_volsw():

int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	/* 得到设置"DAC VOLUME"的寄存器 ES8311_DAC_REG32 */
	unsigned int reg = mc->reg;
	/* 从寄存器 ES8311_DAC_REG32 中读取数据 */
	ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
}

static int snd_soc_read_signed(struct snd_soc_component *component,
	unsigned int reg, unsigned int mask, unsigned int shift,
	unsigned int sign_bit, int *signed_val)
{
	ret = snd_soc_component_read(component, reg, &val);
}

int snd_soc_component_read(struct snd_soc_component *component,
	unsigned int reg, unsigned int *val)
{
	int ret;

	if (component->regmap)
		ret = regmap_read(component->regmap, reg, val);
	else if (component->driver->read) {
		*val = component->driver->read(component, reg);
		ret = 0;
	}
	else
		ret = -EIO;

	return ret;
}

所以,tinymix 读取控件值的完整路径:

用户命令: tinymix "DAC VOLUME"
         ↓
tinymix: mixer_ctl_get_value()
         ↓
系统调用: ioctl(SNDRV_CTL_IOCTL_ELEM_READ)
         ↓
内核: snd_ctl_elem_read_user() → 查找控件 → 调用驱动的 get() 回调
         ↓
驱动: snd_soc_get_volsw() → 读取硬件寄存器
         ↓
硬件: I2C读取 → 返回寄存器值 → 映射为用户值
         ↓
tinymix: 显示 "DAC VOLUME: 150"

设置control的值

整体流程跟读取差不多,代码不再赘述,下面是tinymix 设置控件值的完整路径:

用户命令: tinymix "DAC VOLUME" 192tinymix: mixer_ctl_set_value()系统调用: ioctl(SNDRV_CTL_IOCTL_ELEM_WRITE)内核: snd_ctl_elem_write_user() → 查找控件 → 调用驱动的 put() 回调驱动: snd_soc_put_volsw() → 映射为寄存器值 → I2C写入硬件: CODEC芯片更新DAC增益寄存器实时生效: 后续音频数据使用新的增益系数

C289B571-FC59-480F-B744-7075111137F6_1_105_c.jpeg