1. ALSA主要流程
创建snd_card的实例
初始化snd_card的成员变量
创建声卡的逻辑设备,例如PCM、Control
向系统注册snd_card
2. PCM逻辑设备
2.1 PCM设备的主要功能
playback:将用户程序发来的PCM数据进行D/A转换
capture: 将麦克风或其他输入源的模拟型号进行A/D转换
2.2 snd_card与pcm关系
一个snd_card实例可以包含多个pcm实例
一个pcm实例有一个playback流和一个cpature流组成
playback流由一个或则多个substreams组成
capture流由一个或则多个substreams组成
2.3 snd_pcm结构体
2.3.1 snd_pcm结构体:
structsnd_pcm{structsnd_card*card;//所属声卡structlist_healist;intdevice;/* pcm设备的编号 */...charid[64];charname[80];//streams[0]为playback(播放)//streams[1]为capture(录音)structsnd_pcm_strstreams[2];...};2.3.2 snd_pcm_str的结构体
structsnd_pcm_str{/* 0为playback,1为capture */intstream;structsnd_pcm*pcm;/* -- substreams -- */unsignedintsubstream_count;//substreams的个数unsignedintsubstream_opened;structsnd_pcm_substream*substream;//子流...structdevicedev;};2.3.3 snd_pcm_substream的结构体
structsnd_pcm_substream{structsnd_pcm*pcm;structsnd_pcm_str*pstr;void*private_data;/* copied from pcm->private_data */intnumber;/* 子流编号 */charname[32];/* substream name */intstream;/* stream (direction) */size_tbuffer_bytes_max;/* limit ring buffer size */structsnd_dma_bufferdma_buffer;size_tdma_max;/* 由具体驱动设置ops */conststructsnd_pcm_ops*ops;/* pcm设备运行时的信息 */structsnd_pcm_runtime*runtime;...structsnd_pcm_substream*next;// 指向下一个子流};其中的ops字段用于操作对应的设备,它可以由驱动程序调用snd_pcm_set_ops()函数来设置
runtime字段用于记录重要的软件和硬件运行环境和参数
2.3.4 snd_pcm挂到snd_card下
pcm注册时,会得到一个次设备号,根据这个次设备号,记录到对应的snd_minor数组中。
操作函数替换的主要逻辑如下伪代码所示:
unsignedintminor=iminor(inode);structsnd_minor*mptr=NULL;conststructfile_operations*new_fops;if(minor>=ARAY_SIZE(snd_minors))return-ENODEV;//以次设备号为下标在snd_minors数组中找到对应的snd_minormptr=snd_minors[minor];...//从snd_minor得到file_operationsnew_fops=fops_get(mptr->f_ops);...//替换原来的file_operationsreplace_fops(file,new_fops);if(file->f_op->open)err=file->f_op->open(inode,file);整个过程就是,声卡驱动程序会注册声卡(这只是一个实例,相当于虚拟的设备?用于连接各逻辑设备),然后将对应的pcm设备等的ops设置好。