声卡注册流程(linux-5.4)

结构体介绍

先介绍声卡相关结构体

struct asoc_simple_priv {
        struct snd_soc_card snd_card;
        struct simple_dai_props {
                struct asoc_simple_dai *cpu_dai;
                struct asoc_simple_dai *codec_dai;
                struct snd_soc_dai_link_component cpus;   /* single cpu */
                struct snd_soc_dai_link_component codecs; /* single codec */
                struct snd_soc_dai_link_component platforms;
                struct asoc_simple_data adata;
                struct snd_soc_codec_conf *codec_conf;
                bool mclk_fp;
                unsigned int mclk_fs;
                unsigned int cpu_pll_fs;
                unsigned int codec_pll_fs;
        } *dai_props;
        struct asoc_simple_jack hp_jack;
        struct asoc_simple_jack mic_jack;
        struct snd_soc_dai_link *dai_link;
        struct asoc_simple_dai *dais;
        struct snd_soc_codec_conf *codec_conf;
        struct gpio_desc *pa_gpio;
};

struct snd_soc_card {
        const char *name;
        const char *long_name;
        const char *driver_name;
        char dmi_longname[80];
        char topology_shortname[32];

        struct device *dev;
        struct snd_card *snd_card;
        struct module *owner;

        struct mutex mutex;
        struct mutex dapm_mutex;

        /* Mutex for PCM operations */
        struct mutex pcm_mutex;
        enum snd_soc_pcm_subclass pcm_subclass;

        spinlock_t dpcm_lock;

        bool instantiated;
        bool topology_shortname_created;

        int (*probe)(struct snd_soc_card *card);
        int (*late_probe)(struct snd_soc_card *card);
        int (*remove)(struct snd_soc_card *card);

        /* the pre and post PM functions are used to do any PM work before and
         * after the codec and DAI's do any PM work. */
        int (*suspend_pre)(struct snd_soc_card *card);
        int (*suspend_post)(struct snd_soc_card *card);
        int (*resume_pre)(struct snd_soc_card *card);
        int (*resume_post)(struct snd_soc_card *card);

        /* callbacks */
        int (*set_bias_level)(struct snd_soc_card *,
                              struct snd_soc_dapm_context *dapm,
                              enum snd_soc_bias_level level);
        int (*set_bias_level_post)(struct snd_soc_card *,
                                   struct snd_soc_dapm_context *dapm,
                                   enum snd_soc_bias_level level);

        int (*add_dai_link)(struct snd_soc_card *,
                            struct snd_soc_dai_link *link);
        void (*remove_dai_link)(struct snd_soc_card *,
                            struct snd_soc_dai_link *link);

        long pmdown_time;

        /* CPU <--> Codec DAI links  */
        struct snd_soc_dai_link *dai_link;  /* predefined links only */
        int num_links;  /* predefined links only */
        struct list_head dai_link_list; /* all links */

        struct list_head rtd_list;
        int num_rtd;

        /* optional codec specific configuration */
        struct snd_soc_codec_conf *codec_conf;
        int num_configs;

        /*
         * optional auxiliary devices such as amplifiers or codecs with DAI
         * link unused
         */
        struct snd_soc_aux_dev *aux_dev;
        int num_aux_devs;
        struct list_head aux_comp_list;

        const struct snd_kcontrol_new *controls;
        int num_controls;

        /*
         * Card-specific routes and widgets.
         * Note: of_dapm_xxx for Device Tree; Otherwise for driver build-in.
         */
        const struct snd_soc_dapm_widget *dapm_widgets;
        int num_dapm_widgets;
        const struct snd_soc_dapm_route *dapm_routes;
        int num_dapm_routes;
        const struct snd_soc_dapm_widget *of_dapm_widgets;
        int num_of_dapm_widgets;
        const struct snd_soc_dapm_route *of_dapm_routes;
        int num_of_dapm_routes;
        bool fully_routed;
        bool disable_route_checks;

        /* lists of probed devices belonging to this card */
        struct list_head component_dev_list;
        struct list_head list;

        struct list_head widgets;
        struct list_head paths;
        struct list_head dapm_list;
        struct list_head dapm_dirty;

        /* attached dynamic objects */
        struct list_head dobj_list;

        /* Generic DAPM context for the card */
        struct snd_soc_dapm_context dapm;
        struct snd_soc_dapm_stats dapm_stats;
        struct snd_soc_dapm_update *update;
        
#ifdef CONFIG_DEBUG_FS
        struct dentry *debugfs_card_root;
#endif
#ifdef CONFIG_PM_SLEEP
        struct work_struct deferred_resume_work;
#endif
        u32 pop_time;

        void *drvdata;

        ANDROID_KABI_RESERVE(1);
        ANDROID_KABI_RESERVE(2);
        ANDROID_KABI_RESERVE(3);
        ANDROID_KABI_RESERVE(4);
};

struct snd_card {
        int number;                     /* number of soundcard (index to
                                                                snd_cards) */

        char id[16];                    /* id string of this card */
        char driver[16];                /* driver name */
        char shortname[32];             /* short name of this soundcard */
        char longname[80];              /* name of this soundcard */
        char irq_descr[32];             /* Interrupt description */
        char mixername[80];             /* mixer name */
        char components[128];           /* card components delimited with
                                                                space */
        struct module *module;          /* top-level module */

        void *private_data;             /* private data for soundcard */
        void (*private_free) (struct snd_card *card); /* callback for freeing of
                                                                private data */
        struct list_head devices;       /* devices */

        struct device ctl_dev;          /* control device */
        unsigned int last_numid;        /* last used numeric ID */
        struct rw_semaphore controls_rwsem;     /* controls list lock */
        rwlock_t ctl_files_rwlock;      /* ctl_files list lock */
        int controls_count;             /* count of all controls */
        int user_ctl_count;             /* count of all user controls */
        struct list_head controls;      /* all controls for this card */
        struct list_head ctl_files;     /* active control files */

        struct snd_info_entry *proc_root;       /* root for soundcard specific files */
        struct proc_dir_entry *proc_root_link;  /* number link to real id */

        struct list_head files_list;    /* all files associated to this card */
        struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
                                                                state */
        spinlock_t files_lock;          /* lock the files for this card */
        int shutdown;                   /* this card is going down */
        struct completion *release_completion;
        struct device *dev;             /* device assigned to this card */
        struct device card_dev;         /* cardX object for sysfs */
        const struct attribute_group *dev_groups[4]; /* assigned sysfs attr */
        bool registered;                /* card_dev is registered? */
        wait_queue_head_t remove_sleep;

#ifdef CONFIG_PM
        unsigned int power_state;       /* power state */
        wait_queue_head_t power_sleep;
#endif

#if IS_ENABLED(CONFIG_SND_MIXER_OSS)
        struct snd_mixer_oss *mixer_oss;
        int mixer_oss_change_count;
#endif

        ANDROID_KABI_RESERVE(1);
        ANDROID_KABI_RESERVE(2);
};

可以看出,三个结构体的大致内容:

asoc_simple_priv:

  1. 属于该结构体的snd_soc_card

  2. dai相关

  3. 时钟相关

  4. 耳机、麦克风拔插相关

  5. 功放相关

snd_soc_card:

  1. snd_soc_card的基本信息

  2. 属于该结构体的snd_card

  3. 各种mutex(PCM、DAPM等)

  4. 电源管理相关的操作函数

  5. 回调函数(DAPM、DAI LINK)

  6. DAPM、DAI LINK相关

snd_card:

  1. 声卡的基本信息

  2. 属于该声卡的control设备

  3. proc文件系统的相关配置

  4. sys文件系统的相关配置

流程介绍

probe()

  1. 为asoc_simple_priv分配空间并初始化 --- devm_kzalloc_priv() asoc_simple_init_priv()

  2. 判断pdev->dev的status是否为okay --- of_device_is_available()

  3. 最后跳到进行声卡注册 --- devm_snd_soc_register_card()
    3.1 snd_soc_register_card() --- 初始化card里的内核链表以及mutex,
         3.1.1 然后return snd_soc_bind_card()

snd_soc_bind_card()

  1. 实例化snd_soc_card --- snd_soc_instantiate_card()

snd_soc_instantiate_card()

  1. 初始化snd_soc_dai_link结构体(关于dai匹配)--- soc_init_dai_link()

  2. 初始化dapm,并把dapm->list加到card->dapm_list里面 --- snd_soc_dapm_init()

  3. 绑定dai link--- soc_bind_dai_link()
    3.1 soc_new_pcm_runtime() --- 初始化rtd(该结构体与绑定codec和platform有关)
    3.2 通过snd_soc_find_dai() 和 snd_soc_rtdcom_add()来拿到已经注册的codec和platform并添加到rtd
    3.3 通过soc_add_pcm_runtime()将rtd添加到card

  4. 绑定aux设备(耳机口),也是从component list中找到再给card --- soc_bind_aux_dev()

  5. 都完成都开始创建声卡 --- snd_card_new()

  6. 初始化dubugfs --- soc_init_card_debugfs()

  7. 初始化dapm部分 --- snd_soc_dapm_new_controls()

  8. 初始化声卡(仅一次)--- card->probe

  9. 初始化所有dai用到的component --- soc_probe_link_components()

  10. 初始化所有aux设备 --- soc_probe_aux_devices()

  11. 初始化dai links的时候可能会添加新的dais和dai links,所以还需要再进行一次1和3

  12. 初始化所有的dai link --- soc_probe_link_dais()

  13. 创建、添加PCM设备 --- soc_link_init()

  14. 连接dapm --- snd_soc_dapm_link_dai_widgets() snd_soc_dapm_connect_dai_link_widgets()

  15. 注册control设备 --- snd_soc_add_card_controls()

  16. 注册dapm的route --- snd_soc_dapm_add_routes()

  17. 注册设备树的dapm的route --- snd_soc_dapm_add_routes()

  18. 尝试设置dmi_name --- snd_soc_set_dmi_name()

  19. 设置snd_card->name --- snprintf(card->snd_card->**name, ...)

  20. 执行后部分初始化 --- card->late_probe()

  21. 注册dapm_widget --- snd_soc_dapm_new_widgets()

  22. 注册声卡 --- snd_card_register(card->snd_card)

  23. 检查所有dapm尾指针 --- dapm_mark_endpoints_dirty() snd_soc_dapm_sync()

snd_card_register()

  1. 注册声卡设备 --- device_add(&card->card_dev)

  2. 注册所属声卡的所有设备 --- snd_device_register_all()

  3. 通过 if 判断声卡是否已经注册 --- snd_cards[card->num]

  4. 设置card->id --- snd_card_set_id_no_lock()

  5. 加入到snd_cards[ ]中 --- snd_card[card->num] = card

  6. 注册card的proc文件--- snd_info_card_register()

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Linux 内核声卡注册流程如下: 1. 在内核源码目录的`sound`目录下创建一个新的文件夹,用于存放声卡驱动程序。 2. 在新创建的文件夹中编写声卡驱动程序。 3. 在`sound`目录下的`Makefile`中添加声卡驱动程序的编译规则。 4. 在内核配置文件中启用声卡驱动程序的编译选项。 5. 编译内核。 6. 安装编译好的内核。 7. 加载声卡驱动程序模块。 8. 使用声卡。 请注意,这是一个大致的流程,在实际操作中还可能有其他步骤需要注意。 ### 回答2: Linux内核中的声卡注册流程如下: 1. 驱动程序加载:在Linux启动过程中,声卡驱动程序会被加载到内核中。 2. 平台设备注册声卡驱动程序会通过调用平台设备注册函数将声卡的硬件平台设备注册到内核中。这个平台设备包含了声卡的硬件信息和操作方法。 3. PCM子设备注册声卡驱动程序会创建PCM子设备并将其注册声卡平台设备中。PCM子设备是用于音频数据的输入输出的设备。 4. 控制设备注册声卡驱动程序会创建控制设备并将其注册声卡平台设备中。控制设备用于管理声卡的各种参数和控制操作。 5. 声卡驱动注册声卡驱动程序通过调用声卡驱动注册函数将自己注册到内核中。这个函数会将声卡驱动声卡平台设备进行关联。 6. ALSA框架注册:当声卡驱动注册成功后,会向ALSA(Advanced Linux Sound Architecture)框架注册声卡驱动ALSA框架是一个提供音频功能的软件接口库。 7. OSS兼容性模块注册:为了提供对Open Sound System (OSS)的兼容性,声卡驱动程序会注册一个OSS兼容性模块,使得应用程序能够通过OSS接口访问声卡。 8. 声音设备节点创建:声卡驱动程序会创建声音设备节点,并将其添加到/dev目录下,以供用户空间的应用程序进行访问和控制。 通过以上步骤,Linux内核中的声卡驱动程序成功地注册声卡设备,使得用户可以在应用程序中使用声卡来进行音频输入输出和控制操作。 ### 回答3: Linux内核声卡注册流程主要包括以下几个步骤: 1. 驱动程序加载:首先,内核会加载与声卡相关的驱动程序。这些驱动程序通常存放在内核模块中,可以通过命令行或配置文件来加载。 2. 检测音频硬件:加载完驱动程序后,内核会通过PCI或ACPI等机制来检测系统中的音频硬件设备。这个过程通常由内核的硬件探测模块完成。 3. 设备初始化:一旦检测到音频硬件,内核会对其进行初始化。这包括分配内存,设置寄存器以及配置中断等。 4. 注册设备:接下来,内核会为音频硬件设备分配资源并将其注册到系统中。这样,用户空间程序可以通过设备文件访问声卡。 5. 设置转换规则:内核会根据配置文件或系统默认设置,设置音频转换规则。这些规则包括音频采样率、位深度以及声道数等。 6. 驱动接口注册:内核将声卡驱动程序的操作函数指针注册音频子系统中。这样用户空间的音频应用程序可以通过音频子系统调用驱动程序功能。 7. 中断注册:对于需要使用中断的设备,内核会注册中断处理函数,以便在产生中断时及时响应并处理音频数据。 8. 用户空间配置:最后,用户空间的音频应用程序可以通过音频子系统接口来配置和控制声卡。它们可以打开、关闭、调节音量等。 总的来说,Linux内核声卡注册流程是一个完整的过程,从加载驱动程序到设备初始化、设备注册以及设置转换规则,最后让用户空间程序通过驱动接口和中断来与声卡进行交互。这个过程实现了声卡在操作系统中的正常功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨天不打滑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值