第二章:ALSA核心框架:架构设计思想,card、device、substream概念,PCM与control接口

各位好,欢迎来到第二章。这一章我们聊聊ALSA的核心框架。说实话,很多做音频的工程师用了好几年ALSA,但对它底层的设计思想还是一知半解。我个人觉得,搞懂card、device、substream这三个概念,你就抓住了ALSA的命门。

2.1 ALSA的架构设计思想

ALSA的全称是Advanced Linux Sound Architecture。名字听着挺唬人,但说白了,它就是一个音频设备的抽象层。

为什么要搞这么一层?你想想看,硬件厂商那么多,芯片型号五花八门。如果没有一个统一的框架,每个驱动都自己搞一套接口,那上层应用就疯了。我早期做嵌入式项目时,就见过厂商自己写的音频驱动,API乱七八糟,换个芯片就得重写应用层代码,那叫一个痛苦。

ALSA的设计思想,核心就四个字:分层抽象。它把音频系统分成三层:

  • 硬件层:直接跟芯片寄存器打交道,处理中断、DMA这些底层操作
  • 核心层:提供统一的框架,管理card、device、substream这些逻辑对象
  • 用户层:通过/dev/snd/下的设备文件,给应用程序提供标准接口

这三层之间通过回调函数和数据结构连接。驱动开发者只需要实现核心层定义好的回调,剩下的框架帮你搞定。嗯,这里要注意,回调函数的注册时机和顺序很关键,搞反了系统可能直接崩溃。

核心思想总结:

  • 硬件差异由驱动层隔离
  • 核心层提供统一的数据模型
  • 用户层暴露标准API(如PCM、Control、MIDI等)

下面这张图是我自己画的ALSA核心架构图,你看一眼就明白了:

用户层 (Userspace) 应用程序 (aplay, arecord, 多媒体框架) ↓ 通过 /dev/snd/ 设备文件访问 ALSA核心层 (Core Layer) Card 管理 PCM 设备 Control 接口 MIDI/定时器 ↓ 回调函数 + 数据结构 硬件抽象层 (HAL / Driver) snd_soc_dai_driver snd_soc_codec_driver 平台驱动 (DMA等)

2.2 Card、Device、Substream 概念

这三个概念是ALSA的基石。我见过不少开发者,代码写了一大堆,但问起card和device的区别,支支吾吾说不清楚。咱们一个一个来拆解。

2.2.1 Card(声卡)

Card是ALSA中最顶层的逻辑单元。一个card对应一个物理声卡,或者一个SoC内部的音频子系统。比如你的开发板上有一颗WM8960音频芯片,那它通常就是一个card。

在代码里,card用struct snd_card表示。创建card的典型流程是这样的:

struct snd_card *card;
int err;

/* 创建一个card,id为"mycard",索引为0 */
err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, 
 SNDRV_DEFAULT_STR1, THIS_MODULE, 
 0, &card);
if (err < 0) {
 dev_err(&pdev->dev, "snd_card_new failed: %d\n", err);
 return err;
}

/* 注册card */
err = snd_card_register(card);
if (err < 0) {
 snd_card_free(card);
 return err;
}

小提示:snd_card_new的最后一个参数是私有数据的大小。如果你需要给card挂载一些自定义数据,可以在这里分配。我习惯把platform_device指针存进去,方便后续回调里取用。

2.2.2 Device(设备)

一个card下面可以挂多个device。每个device代表一种功能类型。常见的device类型有:

  • PCM设备:用于播放和录音,编号通常从0开始
  • Control设备:用于音量调节、混音控制等
  • MIDI设备:处理MIDI数据
  • 定时器设备:提供音频相关的定时服务

在用户空间,你看到的/dev/snd/pcmC0D0p这个文件,意思就是:card 0、device 0、playback(p表示播放,c表示录音)。

创建PCM设备的代码示例:

struct snd_pcm *pcm;
int err;

/* 创建PCM设备,playback方向 */
err = snd_pcm_new(card, "my PCM device", 0, /* device编号 */
 1, /* playback流数量 */
 0, /* capture流数量 */
 &pcm);
if (err < 0) {
 dev_err(card->dev, "snd_pcm_new failed: %d\n", err);
 return err;
}

/* 设置PCM操作回调 */
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &my_pcm_ops);

2.2.3 Substream(子流)

Substream是ALSA里最细粒度的概念。一个PCM device可以有多个substream。每个substream代表一个独立的音频数据流。

举个例子:你的声卡支持立体声播放,但上层应用可能同时有两个进程在播放音频。这时候,每个进程可以打开一个substream,硬件通过混音器把它们混合后输出。

Substream的状态机是这样的:

状态 说明 典型触发操作
OPEN 应用打开设备文件 snd_pcm_open()
SETUP 配置参数(采样率、格式等) snd_pcm_hw_params()
PREPARE 准备就绪,等待启动 snd_pcm_prepare()
RUNNING 正在传输数据 snd_pcm_start()
DRAINING 播放完剩余数据后停止 snd_pcm_drain()
STOPPED 停止传输 snd_pcm_stop() / snd_pcm_drop()
SUSPENDED 系统挂起时的状态 电源管理事件

注意:我曾经在一个项目中遇到substream状态切换的问题。应用层调用snd_pcm_drop()后,驱动没有正确清理DMA缓冲区,导致下次打开时还能听到上次残留的音频片段。这个bug查了我整整两天。所以,状态切换时一定要把硬件彻底复位。

2.3 PCM接口详解

PCM接口是ALSA里最常用的接口,负责音频数据的传输。它定义了一组回调函数,驱动开发者需要实现这些回调来跟硬件交互。

核心的回调结构体是struct snd_pcm_ops:

struct snd_pcm_ops {
 int (*open)(struct snd_pcm_substream *substream);
 int (*close)(struct snd_pcm_substream *substream);
 int (*ioctl)(struct snd_pcm_substream *substream,
 unsigned int cmd, void *arg);
 int (*hw_params)(struct snd_pcm_substream *substream,
 struct snd_pcm_hw_params *params);
 int (*hw_free)(struct snd_pcm_substream *substream);
 int (*prepare)(struct snd_pcm_substream *substream);
 int (*trigger)(struct snd_pcm_substream *substream, int cmd);
 snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
 int (*copy)(struct snd_pcm_substream *substream, int channel,
 snd_pcm_uframes_t pos, void __user *buf,
 snd_pcm_uframes_t count);
 int (*silence)(struct snd_pcm_substream *substream,
 int channel, snd_pcm_uframes_t pos,
 snd_pcm_uframes_t count);
 struct page *(*page)(struct snd_pcm_substream *substream,
 unsigned long offset);
 int (*mmap)(struct snd_pcm_substream *substream,
 struct vm_area_struct *vma);
 int (*ack)(struct snd_pcm_substream *substream);
};

这里面,trigger和pointer是最关键的两个回调。trigger负责启动和停止硬件传输,pointer告诉框架当前硬件读/写到了哪个位置。

我写一个简单的trigger实现给你看:

static int my_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
{
 struct my_chip *chip = snd_pcm_substream_chip(substream);

 switch (cmd) {
 case SNDRV_PCM_TRIGGER_START:
 /* 启动DMA传输 */
 my_dma_start(chip);
 break;
 case SNDRV_PCM_TRIGGER_STOP:
 /* 停止DMA传输 */
 my_dma_stop(chip);
 break;
 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 /* 暂停(保留FIFO中的数据) */
 my_dma_pause(chip);
 break;
 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 /* 恢复 */
 my_dma_resume(chip);
 break;
 default:
 return -EINVAL;
 }
 return 0;
}

关键点:trigger回调是在原子上下文中调用的(spinlock持有中),所以里面不能调用任何可能睡眠的函数。比如kmalloc、copy_from_user这些都不行。我见过有人在这里面调了msleep(),结果系统直接死锁。

2.4 Control接口

Control接口是ALSA的"控制面板"。音量调节、静音开关、EQ设置、混音器控制,这些都是通过Control接口实现的。

每个control有一个名字和类型。常见的类型有:

  • BOOLEAN:开关量,比如静音
  • INTEGER:整数值,比如音量(0-100)
  • ENUMERATED:枚举值,比如选择输入源
  • BYTES:原始字节数据,用于传递自定义配置

创建control的代码示例:

static int my_ctl_info(struct snd_kcontrol *kcontrol,
 struct snd_ctl_elem_info *uinfo)
{
 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 uinfo->count = 2; /* 左右声道 */
 uinfo->value.integer.min = 0;
 uinfo->value.integer.max = 100;
 return 0;
}

static int my_ctl_get(struct snd_kcontrol *kcontrol,
 struct snd_ctl_elem_value *ucontrol)
{
 struct my_chip *chip = snd_kcontrol_chip(kcontrol);
 ucontrol->value.integer.value[0] = chip->volume_left;
 ucontrol->value.integer.value[1] = chip->volume_right;
 return 0;
}

static int my_ctl_put(struct snd_kcontrol *kcontrol,
 struct snd_ctl_elem_value *ucontrol)
{
 struct my_chip *chip = snd_kcontrol_chip(kcontrol);
 int changed = 0;

 if (chip->volume_left != ucontrol->value.integer.value[0]) {
 chip->volume_left = ucontrol->value.integer.value[0];
 changed = 1;
 }
 if (chip->volume_right != ucontrol->value.integer.value[1]) {
 chip->volume_right = ucontrol->value.integer.value[1];
 changed = 1;
 }
 return changed;
}

/* 定义control结构体 */
static struct snd_kcontrol_new my_volume_control = {
 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 .name = "Master Playback Volume",
 .info = my_ctl_info,
 .get = my_ctl_get,
 .put = my_ctl_put,
};

/* 注册control */
snd_ctl_add(card, snd_ctl_new1(&my_volume_control, chip));

经验之谈:control的put回调返回值的含义很多人搞错。返回0表示值没变,返回1表示值被修改了。如果你返回0但实际改了硬件寄存器,上层应用可能不会更新缓存,导致下次读到的还是旧值。我踩过这个坑,调试时发现alsa-lib里的音量滑块怎么拖都不动,最后发现是返回值写错了。

2.5 三者之间的关系

最后,我用一个简单的层次图来总结card、device、substream的关系:

Card (snd_card)
 ├── Device 0: PCM (snd_pcm)
 │ ├── Substream 0: Playback
 │ └── Substream 1: Capture
 ├── Device 1: Control (snd_ctl)
 │ ├── Control "Master Volume"
 │ ├── Control "Mic Boost"
 │ └── Control "Input Source"
 └── Device 2: MIDI (snd_rawmidi)
 ├── Substream 0: Input
 └── Substream 1: Output

说白了,card就是你的声卡,device是声卡上的不同功能模块,substream是每个功能模块上的独立数据通道。理解了这个层次,你再看ALSA的代码,就会觉得豁然开朗。

好了,这一章的内容就到这里。下一章我们会深入PCM的数据流管理,包括环形缓冲区、DMA传输、以及xrun的处理。到时候我会分享一个我在车机项目中遇到的xrun调试案例,挺有意思的。


,