上一篇文章我们将了嵌入式系统注册声卡的过程:https://blog.csdn.net/qq_37659294/article/details/104748747
这篇文章我们以打开一个声卡的播放节点为例,讲解一下在APP调用open时,最终会如何调用到硬件相关的函数。
在上一篇文章最后我们说过,当应用程序open设备文件/dev/snd/pcmCxDxp(controlC0和pcmCxDxc也类似,这里我们以pcmCxDxp为例)时,会进入snd_fops的open回调函数,该open函数以次设备号为索引,从snd_minors全局数组中取出当初注册conrol、pcm设备时填充的snd_minor结构体,然后从snd_minor结构中取出control、pcm设备的f_ops,并且把file->f_op替换为pcm设备的f_ops,紧接着调用pcm设备的f_ops->open()(也就是snd_pcm_f_ops[0]的snd_pcm_playback_open)。后面我们根据文件句柄操作这个设备节点时,就都是调用这个f_ops里的函数。
static const struct file_operations snd_fops =
{
.owner = THIS_MODULE,
.open = snd_open,
.llseek = noop_llseek,
};
static int snd_open(struct inode *inode, struct file *file)
{
unsigned int minor = iminor(inode);
struct snd_minor *mptr = NULL;
const struct file_operations *old_fops;
int err = 0;
if (minor >= ARRAY_SIZE(snd_minors))
return -ENODEV;
mutex_lock(&sound_mutex);
mptr = snd_minors[minor]; //根据次设备号找到对应的snd_minor结构体
if (mptr == NULL) {
mptr = autoload_device(minor);
if (!mptr) {
mutex_unlock(&sound_mutex);
return -ENODEV;
}
}
old_fops = file->f_op;
file->f_op = fops_get(mptr->f_ops); //把file->f_op替换为pcm设备的f_ops
if (file->f_op == NULL) {
file->f_op = old_fops;
err = -ENODEV;
}
mutex_unlock(&sound_mutex);
if (err < 0)
return err;
if (file->f_op->open) { //调用pcm设备的f_ops->open()
err = file->f_op->open(inode, file);
if (err) {
fops_put(file->f_op);
file->f_op = fops_get(old_fops);
}
}
fops_put(old_fops);
return err;
}
真正和硬件相关的函数是我们在platform和codec部分放入对应链表且后来又在machine部分根据dai_link实例化的那四个结构体(s3c24xx_i2s_dai、samsung_asoc_platform、uda134x_dai、soc_codec_dev_uda134x)里的函数,所以snd_pcm_f_ops[0]的snd_pcm_playback_open函数又会最终调用到这些硬件相关的操作,下面是调用的过程
2. /dev/snd/pcmC0D0p 对应的file_operations是snd_pcm_f_ops[0]
open : snd_pcm_playback_open
snd_pcm_open
snd_pcm_open_file
struct snd_pcm_substream *substream;
snd_pcm_open_substream
snd_pcm_attach_substream
substream->private_data = pcm->private_data;
err = snd_pcm_hw_constraints_init(substream);
snd_mask_any
snd_interval_any
......
err = substream->ops->open(substream) // substream->ops : snd_pcm_ops结构体
soc_pcm_open
依次调用cpu_dai, dma, codec_dai, machine的open或startup函数,如uda134x_startup、dma_open
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
...
①在《ASOC注册过程》这篇文章的第⑦点中我们提到一点:soc_new_pcm函数里有一个(snd_pcm)pcm->private_data = rtd;后面我们APP调用程序的时候会从pcm->private_data取出rtd(rtd里有对应我们开发板的cpu_dai、codec_dai...)。
现在在snd_pcm_attach_substream函数里,我们就把这个rtd取出来,放到了substream->private_data。
②在《ASOC注册过程》这篇文章的第⑥点,内核给card->rtd->ops结构体里的函数指针赋值并把card->rtd->ops的地址赋给substream->ops。
现在我们调用substream->ops->open(substream),也就是调用snd_pcm_ops结构体里的soc_pcm_open函数。
③在前面第①点中,我们已经把rtd(rtd里有对应我们开发板的cpu_dai、codec_dai...)取出来,放到了substream->private_data,在soc_pcm_open函数里我们通过substream->private_data调用硬件相关的函数,如cpu_dai, dma, codec_dai的open或startup函数。