织梦CMS - 轻松建站从此开始!

罗索

linux2.6.30中uda134x声卡源码解读

落鹤生 发布于 2012-03-02 15:57 点击:次 
主要工作就是遍历声卡list上的所有声卡,然后实例化内部所有通道的pcm,那么具体如何进行实例化的呢,let’s go,(我先把代码贴出来,有些简单的注释,然后会就具体的一些需要重点注意的地方再做个说明)
TAG:

首先我们关注一下源码的位置:sound/soc/s3c24xx/s3c24xx_uda134x.c

下面我们就其中的重要函数进行分析:
由module_init(s3c24xx_uda134x_init);引出:
static int __init s3c24xx_uda134x_init(void)
{
return platform_driver_register(&s3c24xx_uda134x_driver); // 驱动arch/arm/mach-xx下注册的名为"s3c24xx_uda134x"的设备
}
s3c24xx_uda134x_driver结构体如下:
static struct platform_driver s3c24xx_uda134x_driver= {
.probe = s3c24xx_uda134x_probe,
.remove = s3c24xx_uda134x_remove,
.driver = {
   .name = "s3c24xx_uda134x",
   .owner = THIS_MODULE,
},
};
下面我们可以关注下s3c24xx_uda134x_probe函数所做的工作,一步步就每一个语句分析:
s3c24xx_uda134x_l3_pins = pdev->dev.platform_data; // 由platform设备传进来的l3的3个控制io口
/*做了一个gpio口的封装,这段程序应该就是进行l3总线的初始化*/
if (s3c24xx_uda134x_setup_pin(s3c24xx_uda134x_l3_pins->l3_data,
          "data") < 0) // 调用gpio_request申请io
   return -EBUSY;
if (s3c24xx_uda134x_setup_pin(s3c24xx_uda134x_l3_pins->l3_clk,
          "clk") < 0) {
   gpio_free(s3c24xx_uda134x_l3_pins->l3_data);
   return -EBUSY;
}
if (s3c24xx_uda134x_setup_pin(s3c24xx_uda134x_l3_pins->l3_mode,
          "mode") < 0) {
   gpio_free(s3c24xx_uda134x_l3_pins->l3_data);
   gpio_free(s3c24xx_uda134x_l3_pins->l3_clk);
   return -EBUSY;
}
紧接着我们申请一个名为“soc-audio“的设备
s3c24xx_uda134x_snd_device = platform_device_alloc("soc-audio", -1);// 申请名为“soc-audio“设备
platform_set_drvdata(s3c24xx_uda134x_snd_device,// 设备soc-audio“将由soc_driver声卡驱动管理
        &s3c24xx_uda134x_snd_devdata);   // s3c24xx_uda134x_snd_devdata声卡驱动soc_driver使用到的结构体
ret = platform_device_add(s3c24xx_uda134x_snd_device); // 将申请的"soc-audio"设备注册到platform总线,由soc_driver声卡驱动进一步管理,此处触发soc-core.c中的soc-audio的soc_driver结构体的probe.
如 同上面的注释所写,我们这里在注册的时候就会触发soc-audio的soc_driver结构体,而soc_driver结构体是由 s3c24xx_uda134x_snd_devdata进行驱动的,所以在看soc_driver结构体之前我们先来看看 s3c24xx_uda134x_snd_devdata结构体:
static struct snd_soc_device s3c24xx_uda134x_snd_devdata= {
.card= &snd_soc_s3c24xx_uda134x,   // 定义的card声卡
.codec_dev= &soc_codec_dev_uda134x,
.codec_data = &s3c24xx_uda134x,
};
接着我们跟进去看看card声卡是如何定义的:
static struct snd_soc_card snd_soc_s3c24xx_uda134x= {// card声卡的结构体
.name = "S3C24XX_UDA134X",
.platform= &s3c24xx_soc_platform,      // 使用s3c24xx_soc_platform平台
.dai_link= &s3c24xx_uda134x_dai_link, // 使用s3c24xx_uda134x_dai_link中的cpu_dai和codec_dai解码流通道,他们将生成一个pcm实例,然后对声音流数据进行发送和接收处理.
.num_links = 1,
};
这 个地方我们需要关注两点,一是platform,另一个就是dai_link。这两点我们分开来看,首先来看platform,我们使用 s3c24xx_soc_platform平台,这个平台是在sound/soc/s3c24xx/s3c24xx-pcm.c进行定义的,我们进去看 看:
EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);

static int __init s3c24xx_soc_platform_init(void)
{
return snd_soc_register_platform(&s3c24xx_soc_platform); // 登记注册24xx的声卡设备到声卡驱动专用的platform_list链表上
}
这个EXPORT_SYMBOL_GPL就是我们能够使用它的缘由所在了。这个结构体如下:
struct snd_soc_platform s3c24xx_soc_platform= {
.name   = "s3c24xx-audio",
.pcm_ops = &s3c24xx_pcm_ops, // 本platform平台提供的pcm操作方法
.pcm_new = s3c24xx_pcm_new,
.pcm_free = s3c24xx_pcm_free_dma_buffers,
};
好了,至此我们第一点的platform介绍就ok了,下面我们接着来看另一点dai_link。跟进dai_link的结构体看看:
static struct snd_soc_dai_link s3c24xx_uda134x_dai_link= {
.name = "UDA134X",    
.stream_name = "UDA134X",
.codec_dai = &uda134x_dai,    // codec芯片接口控制结构体 详见/sound/soc/codecs/uda134x.c
.cpu_dai = &s3c24xx_i2s_dai, // cpu内置的音频控制单元
.ops = &s3c24xx_uda134x_ops, // pcm实例使用到自定义操作方法集
};
这个结构体我们先看下codec_dai,uda134x_dai位于/sound/soc/codecs/uda134x.c,下面我就进去看看:
同样我们可以看到这样的语句:EXPORT_SYMBOL(uda134x_dai);不解释。
static int __init uda134x_init(void)
{
return snd_soc_register_dai(&uda134x_dai);// 注册codec芯片音频驱动模块codec_dai到dai_list链表上
}
module_init(uda134x_init);
这是init,同样不解释。
下面就是uda134x_dai结构体的具体内容:
struct snd_soc_dai uda134x_dai = {
.name = "UDA134X",
/* playback capabilities */
.playback = {                        // 放音通道
   .stream_name = "Playback",
   .channels_min = 1,
   .channels_max = 2,
   .rates = UDA134X_RATES,
   .formats = UDA134X_FORMATS,
},
/* capture capabilities */
.capture = {                        // 录音通道
   .stream_name = "Capture",
   .channels_min = 1,
   .channels_max = 2,
   .rates = UDA134X_RATES,
   .formats = UDA134X_FORMATS,
},
/* pcm operations */
.ops = &uda134x_dai_ops,
};
至此,我们对s3c24xx_uda134x_snd_devdata结构体的认识就告一段落了,下面我们就去关注一下soc_driver结构体,它的源码位于sound/soc/soc_core.c中
从module_init(snd_soc_init);引出
通过下面函数进行init
static int __init snd_soc_init(void)
{
#ifdef CONFIG_DEBUG_FS
debugfs_root = debugfs_create_dir("asoc", NULL);
if (IS_ERR(debugfs_root) || !debugfs_root) {
   printk(KERN_WARNING
         "ASoC: Failed to create debugfs directory\n");
   debugfs_root = NULL;
}
#endif

return platform_driver_register(&soc_driver);
}
进而我们来了解一下soc_driver结构体:
/*s3c24xx_uda134x.c中s3c24xx_uda134x_probe==>platform_device_add(s3c24xx_uda134x_snd_device);
*将直接触发这里soc_probe检测函数的进一步执行
*/

/* ASoC platform driver */
static struct platform_driver soc_driver= {
.driver   = {
   .name   = "soc-audio",
   .owner   = THIS_MODULE,
},
.probe   = soc_probe,
.remove   = soc_remove,
.suspend = soc_suspend,
.resume   = soc_resume,
};
这里我们可以看到我们所说的soc-audio设备就是由这边的soc_driver进行管理的。下面我们来了解一下soc_probe都做了些什么工作。
/* probes a new socdev */
static int soc_probe(struct platform_device *pdev)
{
int ret = 0;
struct snd_soc_device *socdev = platform_get_drvdata(pdev);  // 对应上面的s3c24xx_uda134x_snd_devdata结构体
struct snd_soc_card *card = socdev->card; // 对应上面的snd_soc_s3c24xx_uda134x

/* Bodge while we push things out of socdev */
card->socdev = socdev;

/* Bodge while we unpick instantiation */
card->dev = &pdev->dev;
ret = snd_soc_register_card(card);  // 注册生成声卡
if (ret != 0) {
   dev_err(&pdev->dev, "Failed to register card\n");
   return ret;
}

return 0;
}
我们可以看出,这个probe函数主要是做了一个注册生成声卡的操作,那么这个声卡究竟是怎样注册生成的呢,我们跟进去看看
static int snd_soc_register_card(struct snd_soc_card *card) // 注册生成声卡
{
if (!card->name || !card->dev)
   return -EINVAL;

INIT_LIST_HEAD(&card->list);
card->instantiated = 0;       // 标识未初始化该声卡,因为该函数只在soc_probe中调用,所以可以保证确实是第1次创建

mutex_lock(&client_mutex);
list_add(&card->list, &card_list); // 所有注册的声卡都要添加到声卡设备链表card_list
    // 对声卡进行实例化,生成pcm.
mutex_unlock(&client_mutex);

dev_dbg(card->dev, "Registered card '%s'\n", card->name);

return 0;
}
这个函数里面我们需要关注的就是对声卡进行实例化,生成pcm的snd_soc_instantiate_cards()函数,下面我进去看看:
/*
* Attempt to initialise any uninitalised cards. Must be called with
* client_mutex.
*/
static void snd_soc_instantiate_cards(void)   // 对声卡进行实例化,生成pcm.
{
struct snd_soc_card *card;
list_for_each_entry(card, &card_list, list) // 遍历声卡链表card_list上所有声卡,如果该声卡的instantiated等于0,那么将被执行实例化,生成该声卡描述的所有pcm流通道.
   snd_soc_instantiate_card(card);// 实例化声卡内部所有stream流通道为pcm
}
这个函数的主要工作就是遍历声卡list上的所有声卡,然后实例化内部所有通道的pcm,那么具体如何进行实例化的呢,let’s go,(我先把代码贴出来,有些简单的注释,然后会就具体的一些需要重点注意的地方再做个说明)
static void snd_soc_instantiate_card(struct snd_soc_card *card)// 作声卡实例化
{
struct platform_device *pdev = container_of(card->dev,
          struct platform_device,
          dev);
struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
struct snd_soc_platform *platform;
struct snd_soc_dai *dai;
int i, found, ret, ac97;

if (card->instantiated) // 该card声卡已经完成实例化,简单的返回
   return;

found = 0;
list_for_each_entry(platform, &platform_list, list)// 搜索platform_list上登记的s3c24xx_soc_platform平台驱动
   if (card->platform == platform) {
    found = 1;
    break;
   }
if (!found) {
   dev_dbg(card->dev, "Platform %s not registered\n",
    card->platform->name);
   return;
}

ac97 = 0;
for (i = 0; i < card->num_links; i++) {
   found = 0;
   list_for_each_entry(dai, &dai_list, list)// 检查dai_list是否已经注册登记了.cpu_dai引用到的驱动模块
    if (card->dai_link[i].cpu_dai == dai) { // 这里就是s3c24xx_uda134x_dai_link中的.cpu_dai = &s3c24xx_i2s_dai
     found = 1;                           // 他在s3c24xx_i2s_init中完成注册登记
     break;
    }
   if (!found) {
    dev_dbg(card->dev, "DAI %s not registered\n",
     card->dai_link[i].cpu_dai->name);
    return;
   }

   if (card->dai_link[i].cpu_dai->ac97_control)
    ac97 = 1;
}

/* If we have AC97 in the system then don't wait for the
* codec. This will need revisiting if we have to handle
* systems with mixed AC97 and non-AC97 parts. Only check for
* DAIs currently; we can't do this per link since some AC97
* codecs have non-AC97 DAIs.
*/
if (!ac97)// 如果不是ac97设备
   for (i = 0; i < card->num_links; i++) {
    found = 0;
    list_for_each_entry(dai, &dai_list, list)
     if (card->dai_link[i].codec_dai == dai) {// 检查dai_list是否已经注册登记了.codec_dai引用到的驱动模块
      found = 1;                            // 这里就是s3c24xx_uda134x_dai_link中的.codec_dai = &uda134x_dai
      break;                                // 他在uda134x_init中完成注册登记.
     }
    if (!found) {
     dev_dbg(card->dev, "DAI %s not registered\n",
      card->dai_link[i].codec_dai->name);
     return;
    }
   }

/* Note that we do not current check for codec components */

dev_dbg(card->dev, "All components present, instantiating\n");

// ok,所有依赖的模块都已经就位,那么下面开始创建card声卡.

/* Found everything, bring it up */
if (card->probe) {
   ret = card->probe(pdev);               // 对于snd_soc_s3c24xx_uda134x没有定义probe
   if (ret < 0)
    return;
}

for (i = 0; i < card->num_links; i++) {
   struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
   if (cpu_dai->probe) {
    ret = cpu_dai->probe(pdev, cpu_dai); // cpu集成的音频控制单元probe初始化,s3c24xx_i2s_dai.s3c24xx_i2s_probe
    if (ret < 0)                         // 主要完成i2s控制器时钟启动和s3c24xx的i2s对应的io口配置初始化
     goto cpu_dai_err;
   }
}
// 这里将完成设备节点'/dev/dsp'和alsa节点的所有创建工作
if (codec_dev->probe) {            // soc_codec_dev_uda134x.soc_codec_dev_uda134x.uda134x_soc_probe初始化
   ret = codec_dev->probe(pdev);   // 将执行snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
   if (ret < 0)                    // 生成声卡内存结构体和创建所有pcms流通道,最后调用snd_soc_init_card(socdev);
    goto cpu_dai_err;            // 根据dsp_map[]索引号选择一个默认pcm流通道生成"/dev/dsp"节点和alsa节点
}

if (platform->probe) {                // s3c24xx_soc_platform没有提供probe来初始化
   ret = platform->probe(pdev);
   if (ret < 0)
    goto platform_err;
}

/* DAPM stream work */
INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
#ifdef CONFIG_PM
/* deferred resume work */
INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
#endif

card->instantiated = 1;

return;

platform_err:
if (codec_dev->remove)
   codec_dev->remove(pdev);

cpu_dai_err:
for (i--; i >= 0; i--) {
   struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
   if (cpu_dai->remove)
    cpu_dai->remove(pdev, cpu_dai);
}

if (card->remove)
   card->remove(pdev);
}

(xdyang1986)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201203/15779.html]
本文出处:百度博客 作者:xdyang1986 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容