SM6225 alsa详解(二)_ pcm设备

本文转载于:https://www.cnblogs.com/xinghuo123/p/13121440.html

1 、pcm设备介绍

PCM是英文Pulse-code modulation的缩写,中文译名是脉冲编码调制.我们知道在现实生活中,人耳听到的声音是模拟信号,PCM就是要把声音从模拟转换成数字信号的一种技术,他的原理简单地说就是利用一个固定的频率对模拟信号进行采样,采样后的信号在波形上看就像一串连续的幅值不一的脉冲,把这些脉冲的幅值按一定的精度进行量化,这些量化后的数值被连续地输出、传输、处理或记录到存储介质中,所有这些组成了数字音频的产生过程。
在这里插入图片描述
PCM信号的两个重要指标是采样频率和量化精度,目前,CD音频的采样频率通常为44100Hz,量化精度是16bit.:采样率就是一秒所采集的数据个数,精度16bit代表用16位来表示所采集的数据 16bit
通常,播放音乐时,应用程序从存储介质中读取音频数据(MP3、WMA、AAC…),经过解码后,最终送到音频驱动程序中的就是PCM数据,反过来,在录音时,音频驱动不停地把采样所得的PCM数据送回给应用程序,由应用程序完成压缩、存储等任务.所以,音频驱动的两大核心任务就是:

playback    如何把用户空间的应用程序发过来的PCM数据,转化为人耳可以辨别的模拟音频
capture     把mic拾取到得模拟信号,经过采样、量化,转换为PCM信号送回给用户空间的应用程序

2、 alsa drive中的pcm中间层

ALSA已经为我们实现了功能强劲的PCM中间层,自己的驱动中只要实现一些底层的需要访问硬件的函数即可。要访问PCM的中间层代码,你首先要包含头文件<sound/pcm.h>,另外,如果需要访问一些与 hw_param相关的函数,可能也要包含<sound/pcm_params.h>。

每个声卡最多可以包含4个pcm的实例,每个pcm实例对应一个pcm设备文件.pcm实例数量的这种限制源于linux设备号所占用的位大小,如果以后使用64位的设备号,我们将可以创建更多的pcm实例.不过大多数情况下,在嵌入式设备中,一个pcm实例已经足够了。一个pcm实例由一个playback stream和一个capture stream组成,这两个stream又分别有一个或多个substreams组成。
在这里插入图片描述

3、pcm的数据结构

3.1 struct snd_pcm pcm结构体定义

// sm6225_android13.0_r01_r003/target/kernel_platform/msm-kernel/include/sound/pcm.h
struct snd_pcm {
	struct snd_card *card;
	struct list_head list;
	int device; /* device number */
	unsigned int info_flags;
	unsigned short dev_class;
	unsigned short dev_subclass;
	char id[64];
	char name[80];
	struct snd_pcm_str streams[2];  //该数组中的两个元素指向两个snd_pcm_str结构,分别代表playback stream和capture stream
	struct mutex open_mutex;
	wait_queue_head_t open_wait;
	void *private_data;
	void (*private_free) (struct snd_pcm *pcm);
	bool internal; /* pcm is for internal use only */
	bool nonatomic; /* whole PCM operations are in non-atomic context */
	bool no_device_suspend; /* don't invoke device PM suspend */
#if IS_ENABLED(CONFIG_SND_PCM_OSS)
	struct snd_pcm_oss oss;
#endif
	ANDROID_KABI_RESERVE(1);
};

3.2 struct snd_pcm_str
主要查看snd_pcm_str 这个结构体

struct snd_pcm_str {
	int stream;				/* stream (direction) */
	struct snd_pcm *pcm;
	/* -- substreams -- */
	unsigned int substream_count;
	unsigned int substream_opened;
	struct snd_pcm_substream *substream;  //指向snd_pcm_substream结构
#if IS_ENABLED(CONFIG_SND_PCM_OSS)
	/* -- OSS things -- */
	struct snd_pcm_oss_stream oss;
#endif
#ifdef CONFIG_SND_VERBOSE_PROCFS
	struct snd_info_entry *proc_root;
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
	unsigned int xrun_debug;	/* 0 = disabled, 1 = verbose, 2 = stacktrace */
#endif
#endif
	struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
	struct device dev;
	ANDROID_KABI_RESERVE(1);
};

3.3 struct snd_pcm_substream
是pcm中间层的核心,绝大部分任务都是在substream中处理,尤其是他的ops(snd_pcm_ops)字段,许多user空间的应用程序通过alsa-lib对驱动程序的请求都是由该结构中的函数处理.它的runtime字段则指向snd_pcm_runtime结构,snd_pcm_runtime记录这substream的一些重要的软件和硬件运行环境和参数。

struct snd_pcm_substream {
	struct snd_pcm *pcm;
	struct snd_pcm_str *pstr;
	void *private_data;		/* copied from pcm->private_data */
	int number;
	char name[32];			/* substream name */
	int stream;			/* stream (direction) */
	struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
	size_t buffer_bytes_max;	/* limit ring buffer size */
	struct snd_dma_buffer dma_buffer;
	size_t dma_max;
	/* -- hardware operations -- */
	const struct snd_pcm_ops *ops;  //user空间的应用程序通过alsa-lib对驱动程序的请求都是由该结构中的函数处理
	/* -- runtime information -- */
	struct snd_pcm_runtime *runtime;  //记录这substream的一些重要的软件和硬件运行环境和参数
        /* -- timer section -- */
	struct snd_timer *timer;		/* timer */
	unsigned timer_running: 1;	/* time is running */
	long wait_time;	/* time in ms for R/W to wait for avail */
	/* -- next substream -- */
	struct snd_pcm_substream *next;
	/* -- linked substreams -- */
	struct list_head link_list;	/* linked list member */
	struct snd_pcm_group self_group;	/* fake group for non linked substream (with substream lock inside) */
	struct snd_pcm_group *group;		/* pointer to current group */
	/* -- assigned files -- */
	int ref_count;
	atomic_t mmap_count;
	unsigned int f_flags;
	void (*pcm_release)(struct snd_pcm_substream *);
	struct pid *pid;
#if IS_ENABLED(CONFIG_SND_PCM_OSS)
	/* -- OSS things -- */
	struct snd_pcm_oss_substream oss;
#endif
#ifdef CONFIG_SND_VERBOSE_PROCFS
	struct snd_info_entry *proc_root;
#endif /* CONFIG_SND_VERBOSE_PROCFS */
	/* misc flags */
	unsigned int hw_opened: 1;
	unsigned int managed_buffer_alloc:1;
	ANDROID_VENDOR_DATA(1);
	ANDROID_KABI_RESERVE(1);
};

#define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)


struct snd_pcm_str {
	int stream;				/* stream (direction) */
	struct snd_pcm *pcm;
	/* -- substreams -- */
	unsigned int substream_count;
	unsigned int substream_opened;
	struct snd_pcm_substream *substream;  //指向snd_pcm_substream结构
#if IS_ENABLED(CONFIG_SND_PCM_OSS)
	/* -- OSS things -- */
	struct snd_pcm_oss_stream oss;
#endif
#ifdef CONFIG_SND_VERBOSE_PROCFS
	struct snd_info_entry *proc_root;
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
	unsigned int xrun_debug;	/* 0 = disabled, 1 = verbose, 2 = stacktrace */
#endif
#endif
	struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
	struct device dev;
	ANDROID_KABI_RESERVE(1);
};

3.4 struct snd_pcm_ops
pcm设备的设备操作结构体 就是通过snd_pcm结构体调用snd_pcm_str (分为playback capture)注册的snd_substream里面的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,      /* 用于实现几个特定的IOCTL1_{RESET,INFO,CHANNEL_INFO,GSTATE,FIFO_SIZE} */
		     unsigned int cmd, void *arg);
	int (*hw_params)(struct snd_pcm_substream *substream,   /* 用于设定pcm参数,如采样率/位深... */
			 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);
	int (*sync_stop)(struct snd_pcm_substream *substream);
	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
	int (*get_time_info)(struct snd_pcm_substream *substream,
			struct timespec64 *system_ts, struct timespec64 *audio_ts,
			struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
			struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
	int (*fill_silence)(struct snd_pcm_substream *substream, int channel,
			    unsigned long pos, unsigned long bytes);
	int (*copy_user)(struct snd_pcm_substream *substream, int channel,
			 unsigned long pos, void __user *buf,
			 unsigned long bytes);
	int (*copy_kernel)(struct snd_pcm_substream *substream, int channel,
			   unsigned long pos, void *buf, unsigned long bytes);
	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);
	ANDROID_KABI_RESERVE(1);
}

通过调用文件操作对音频stream进行对应操作,这些操作再对应的codec和音频驱动中会实现
1、open函数为PCM模块设定支持的传输模式、数据格式、通道数、period等参数,并为playback/capture stream分配相应的DMA通道。
2、hw_params函数为substream(每打开一个playback或capture,ALSA core均产生相应的一个substream)设定DMA的源(目的)地址,以及DMA缓冲区的大小。
3、prepare函数当pcm“准备好了”调用该函数。在这里根据channels、buffer_bytes等来设定DMA传输参数,跟具体硬件平台相关。注:每次调用snd_pcm_prepare()的时候均会调用prepare函数。
当pcm开始、停止、暂停的时候都会调用trigger函数。

3.5 struct snd_pcm_runtime
snd_pcm_runtime函数说明:我们会留意到ops各成员函数均需要取得一个snd_pcm_runtime结构体指针,这个指针可以通过substream->runtime来获得。snd_pcm_runtime是pcm运行时的信息。当打开一个pcm子流时,pcm运行时实例就会分配给这个子流。它拥有很多信息:hw_params和sw_params配置拷贝,缓冲区指针,mmap记录,自旋锁等。snd_pcm_runtime对于驱动程序操作集函数是只读的,仅pcm中间层可以改变或更新这些信息

struct snd_pcm_runtime {
	/* -- Status -- */
	struct snd_pcm_substream *trigger_master;
	struct timespec64 trigger_tstamp;	/* trigger timestamp */
	bool trigger_tstamp_latched;     /* trigger timestamp latched in low-level driver/hardware */
	int overrange;
	snd_pcm_uframes_t avail_max;
	snd_pcm_uframes_t hw_ptr_base;	/* Position at buffer restart */
	snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
	unsigned long hw_ptr_jiffies;	/* Time when hw_ptr is updated */
	unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
	snd_pcm_sframes_t delay;	/* extra delay; typically FIFO size */
	u64 hw_ptr_wrap;                /* offset for hw_ptr due to boundary wrap-around */

	/* -- HW params -- */
	snd_pcm_access_t access;	/* access mode */
	snd_pcm_format_t format;	/* SNDRV_PCM_FORMAT_* */
	snd_pcm_subformat_t subformat;	/* subformat */
	unsigned int rate;		/* rate in Hz */  //采样率
	unsigned int channels;		/* channels */   //通道数
	snd_pcm_uframes_t period_size;	/* period size */ 
	unsigned int periods;		/* periods */
	snd_pcm_uframes_t buffer_size;	/* buffer size */
	snd_pcm_uframes_t min_align;	/* Min alignment for the format */
	size_t byte_align;
	unsigned int frame_bits;
	unsigned int sample_bits;
	unsigned int info;
	unsigned int rate_num;
	unsigned int rate_den;
	unsigned int no_period_wakeup: 1;

	/* -- SW params -- */
	int tstamp_mode;		/* mmap timestamp is updated */
  	unsigned int period_step;
	snd_pcm_uframes_t start_threshold;
	snd_pcm_uframes_t stop_threshold;
	snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
						noise is nearest than this */
	snd_pcm_uframes_t silence_size;	/* Silence filling size */
	snd_pcm_uframes_t boundary;	/* pointers wrap point */

	snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
	snd_pcm_uframes_t silence_filled; /* size filled with silence */

	union snd_pcm_sync_id sync;	/* hardware synchronization ID */

	/* -- mmap -- */
	struct snd_pcm_mmap_status *status;
	struct snd_pcm_mmap_control *control;

	/* -- locking / scheduling -- */
	snd_pcm_uframes_t twake; 	/* do transfer (!poll) wakeup if non-zero */
	wait_queue_head_t sleep;	/* poll sleep */
	wait_queue_head_t tsleep;	/* transfer sleep */
	struct fasync_struct *fasync;
	bool stop_operating;		/* sync_stop will be called */
	struct mutex buffer_mutex;	/* protect for buffer changes */
	atomic_t buffer_accessing;	/* >0: in r/w operation, <0: blocked */

	/* -- private section -- */
	void *private_data;
	void (*private_free)(struct snd_pcm_runtime *runtime);

	/* -- hardware description -- */
	struct snd_pcm_hardware hw;
	struct snd_pcm_hw_constraints hw_constraints;

	/* -- timer -- */
	unsigned int timer_resolution;	/* timer resolution */
	int tstamp_type;		/* timestamp type */

	/* -- DMA -- */           
	unsigned char *dma_area;	/* DMA area */
	dma_addr_t dma_addr;		/* physical bus address (not accessible from main CPU) */
	size_t dma_bytes;		/* size of DMA area */

	struct snd_dma_buffer *dma_buffer_p;	/* allocated buffer */
	unsigned int buffer_changed:1;	/* buffer allocation changed; set only in managed mode */

	/* -- audio timestamp config -- */
	struct snd_pcm_audio_tstamp_config audio_tstamp_config;
	struct snd_pcm_audio_tstamp_report audio_tstamp_report;
	struct timespec64 driver_tstamp;

#if IS_ENABLED(CONFIG_SND_PCM_OSS)
	/* -- OSS things -- */
	struct snd_pcm_oss_runtime oss;
#endif
	ANDROID_KABI_RESERVE(1);
	ANDROID_KABI_RESERVE(2);
};

3.6 struct snd_pcm_hardware

/*
 *  Hardware (lowlevel) section
 */

struct snd_pcm_hardware {
	unsigned int info;		/* SNDRV_PCM_INFO_* */
	u64 formats;			/* SNDRV_PCM_FMTBIT_* */
	unsigned int rates;		/* SNDRV_PCM_RATE_* */
	unsigned int rate_min;		/* min rate */
	unsigned int rate_max;		/* max rate */
	unsigned int channels_min;	/* min channels */
	unsigned int channels_max;	/* max channels */
	size_t buffer_bytes_max;	/* max buffer size */
	size_t period_bytes_min;	/* min period size */
	size_t period_bytes_max;	/* max period size */
	unsigned int periods_min;	/* min # of periods */
	unsigned int periods_max;	/* max # of periods */
	size_t fifo_size;		/* fifo size in bytes */
};

3.7 以上pcm各数据结构的关系如下:
在这里插入图片描述
贴一下自己的理解
在这里插入图片描述

4 创建pcm设备

上面讲了PCM各结构体关系,接下来讲创建一个PCM设备
4.1 snd_pcm_new

//sm6225_android13.0_r01_r003/target/kernel_platform/msm-kernel/sound/core/pcm.c
/**
 * snd_pcm_new - create a new PCM instance   创建一个PCM 实例
 * @card: the card instance   				参数:card 声卡实例在第一节中使用snd_card_new中的 snd_card 指针
 * @id: the id string  	s
 * @device: the device index (zero based)  			//device 表示目前创建的是该声卡下的第几个pcm,第一个pcm设备从0开始.
 * @playback_count: the number of substreams for playback  //当前PCM中扬声器的substream数量
 * @capture_count: the number of substreams for capture   //当前PCM中麦克风的substream数量
 * @rpcm: the pointer to store the new pcm instance
 *
 * Creates a new PCM instance.
 *
 * The pcm operators have to be set afterwards to the new instance
 * via snd_pcm_set_ops().
 *
 * Return: Zero if successful, or a negative error code on failure.
 */
int snd_pcm_new(struct snd_card *card, const char *id, int device,
		int playback_count, int capture_count, struct snd_pcm **rpcm)
{
	return _snd_pcm_new(card, id, device, playback_count, capture_count,
			false, rpcm);
}
EXPORT_SYMBOL(snd_pcm_new);

调用函数 _snd_pcm_new

调用该api创建一个pcm,然后会做以下事情:

(1)如果有,建立playback stream,相应的substream也同时建立

(2)如果有,建立capture stream,相应的substream也同时建立

(3)调用snd_device_new()把该pcm挂到声卡中,参数ops中的dev_register字段指向了函数snd_pcm_dev_register,这个回调函数会在声卡的注册阶段被调用.

static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
		int playback_count, int capture_count, bool internal,
		struct snd_pcm **rpcm)
{
	struct snd_pcm *pcm;   //创建一个PCM
	int err;
	static struct snd_device_ops ops = {
		.dev_free = snd_pcm_dev_free,
		.dev_register =	snd_pcm_dev_register,
		.dev_disconnect = snd_pcm_dev_disconnect,
	};

	if (snd_BUG_ON(!card))
		return -ENXIO;
	if (rpcm)
		*rpcm = NULL;
	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
	if (pcm == NULL) {
		dev_err(card->dev, "Cannot allocate PCM\n");
		return -ENOMEM;
	}
	pcm->card = card;
	pcm->device = device;
	pcm->internal = internal;
	if (id)
		strlcpy(pcm->id, id, sizeof(pcm->id));
	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) { //创建playback stream 并根据playback_count 创建多个playback substream 
		snd_pcm_free(pcm);
		return err;
	}
	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {	//创建capture stream 并根据capture_count 创建多个playback substream
		snd_pcm_free(pcm);
		return err;
	}
	mutex_init(&pcm->open_mutex);
	init_waitqueue_head(&pcm->open_wait);
	if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {  //把该pcm挂到声卡中,参数ops中的dev_register字段指向了函数snd_pcm_dev_register,这个回调函数会在声卡的注册阶段被调用
		snd_pcm_free(pcm);
		return err;
	}
	if (rpcm)
		*rpcm = pcm;
	return 0;
}

4.2 snd_device_new函数
作用: (1)拿到声卡逻辑设备的设备操作结构体
(2)将该设备逻辑设备加入声卡结构体devices链表中(该声卡下逻辑设备比如control、pcm等),dev->list加入card->devies,在注册时根据此链表找到对应逻辑设备,调用其注册函数。

//sm6225_android13.0_r01_r003/target/kernel_platform/msm-kernel/sound/core/devie.c
/**
 * snd_device_new - create an ALSA device component
 * @card: the card instance        //当前声卡实例
 * @type: the device type, SNDRV_DEV_XXX   // PCM control 等设备类型
 * @device_data: the data pointer of this device 
 * @ops: the operator table			//card——device Ops
 *
 * Creates a new device component for the given data pointer.
 * The device will be assigned to the card and managed together
 * by the card.
 *
 * The data pointer plays a role as the identifier, too, so the
 * pointer address must be unique and unchanged.
 *
 * Return: Zero if successful, or a negative error code on failure.
 */
int snd_device_new(struct snd_card *card, enum snd_device_type type,
		   void *device_data, struct snd_device_ops *ops)
{
	struct snd_device *dev;
	struct list_head *p;

	if (snd_BUG_ON(!card || !device_data || !ops))
		return -ENXIO;
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (dev == NULL) {
		dev_err(card->dev, "Cannot allocate device, type=%d\n", type);
		return -ENOMEM;
	}
	INIT_LIST_HEAD(&dev->list);
	// 填充snd_dev 结构体
	dev->card = card;
	dev->type = type;  //dev类型 PCM contorl等等
	dev->state = SNDRV_DEV_BUILD;
	dev->device_data = device_data;
	dev->ops = ops;  //该ops就是 _snd_pcm_new或者是其他设备创建是的ops 有free Register等操作 //拿到声卡逻辑设备的设备操作结构体

	/* insert the entry in an incrementally sorted list */
	list_for_each_prev(p, &card->devices) {  //将该设备 逻辑设备加入声卡结构体devices链表中(该声卡下逻辑设备比如control、pcm等),dev->list加入card->devies
		struct snd_device *pdev = list_entry(p, struct snd_device, list);  //获取声卡逻辑设备结构体
		if ((unsigned int)pdev->type <= (unsigned int)type)  //判断该逻辑设备的类型,常用的control或者pcm
			break;
	}

	list_add(&dev->list, p);  //在链表p的前面加入链表dev->list
	return 0;
}
EXPORT_SYMBOL(snd_device_new);

这样我们创建好了PCM设备,并调用 snd_device_new把PCM设备挂载到dev链表中 list_for_each_prev 函数就是可以在后面一直挂设备(PCM control…第一节中说讲的设备类型)并可以调用PCM设备的ops
设备操作结构体定义,位于_snd_pcm_new函数中,最重要的是注册函数

static struct snd_device_ops ops = {
.dev_free = snd_pcm_dev_free,
.dev_register = snd_pcm_dev_register,
.dev_disconnect = snd_pcm_dev_disconnect,
}
以上我们完成了图示过程
在这里插入图片描述
4.3 函数snd_pcm_set_ops
设置操作该pcm的控制/操作接口函数,参数中的snd_pcm_ops结构中的函数通常就是我们驱动要实现的函数。

**
 * snd_pcm_set_ops - set the PCM operators
 * @pcm: the pcm instance
 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
 * @ops: the operator table   //该ops是每一个驱动中要实现的ops 要实现regisetr 等
 *
 * Sets the given PCM operators to the pcm instance.
 */
void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
		     const struct snd_pcm_ops *ops)
{
	struct snd_pcm_str *stream = &pcm->streams[direction];
	struct snd_pcm_substream *substream;
	
	for (substream = stream->substream; substream != NULL; substream = substream->next)
		substream->ops = ops;  //设置每一个 pcm stream —— > substream的ops
}

5 注册pcm设备

在上面创建的pcm设备信息基础上,注册pcm设备。

先调用函数snd_card_register注册声卡//此函数看第一节中 使用函数snd_device_register_all 中会调用snd_device_ops 中的snd_pcm_dev_register函数:ops如下 为_snd_pcm_new中定义
static struct snd_device_ops ops = {
.dev_free = snd_pcm_dev_free,
.dev_register = snd_pcm_dev_register,
.dev_disconnect = snd_pcm_dev_disconnect,
}
并在snd_pcm_dev_register函数中 注册pcm设备

5.1 声卡注册函数
snd_card_register 详见:linux - alsa详解1概括。== dev/snd/pcmCxxDxxp、pcmCxxDxxc ==,设备文件节点的建立。注意和上面pcm设备注册函数的区别,声卡注册函数最终会调用pcm注册函数注册pcm设备,或者调用control设备注册函数注册控制设备。
snd_card_register 函数调用函数snd_device_register_all

调用== snd_device_register_all ==

/*
 * register all the devices on the card.
 * called from init.c
 */
int snd_device_register_all(struct snd_card *card)
{
	struct snd_device *dev;
	int err;
	
	if (snd_BUG_ON(!card))
		return -ENXIO;
	list_for_each_entry(dev, &card->devices, list) {  //遍历声卡结构体中的设备链表,逐个注册声卡下挂载的逻辑设备
		err = __snd_device_register(dev);
		if (err < 0)
			return err;
	}
	return 0;
}

调用 __snd_device_register

static int __snd_device_register(struct snd_device *dev)
{
	if (dev->state == SNDRV_DEV_BUILD) {
		if (dev->ops->dev_register) {
			int err = dev->ops->dev_register(dev);   //调用声卡逻辑设备的注册函数,即pcm设备注册函数snd_pcm_dev_register
			if (err < 0)
				return err;
		}
		dev->state = SNDRV_DEV_REGISTERED;
	}
	return 0;
}

== 以上可以看到在注册声卡的时候 就会遍历 我们之前加上去的pcm等设备,并通过实现dev->ops->dev_register调用我们创建PCM函数中的ops ->dev_register ==
5.2 pcm设备注册函数
以上调用了snd_pcm_dev_register

sm6225_android13.0_r01_r003/target/kernel_platform/msm-kernel/sound/core/pcm.c
//参数snd_device是 4.2节中snd_device_new 中创建的dev
static int snd_pcm_dev_register(struct snd_device *device)
{
	int cidx, err;
	struct snd_pcm_substream *substream;
	struct snd_pcm *pcm;

	if (snd_BUG_ON(!device || !device->device_data))
		return -ENXIO;
	pcm = device->device_data;

	mutex_lock(&register_mutex);
	err = snd_pcm_add(pcm);
	if (err)
		goto unlock;
	for (cidx = 0; cidx < 2; cidx++) {
		int devtype = -1;
		if (pcm->streams[cidx].substream == NULL)
			continue;
		switch (cidx) {
		case SNDRV_PCM_STREAM_PLAYBACK:
			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
			break;
		case SNDRV_PCM_STREAM_CAPTURE:
			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
			break;
		}
		/* register pcm */ //实际就是调用此函数注册社备
		err = snd_register_device(devtype, pcm->card, pcm->device,  //指定pcm设备的文件操作结构体
					  &snd_pcm_f_ops[cidx], pcm,
					  &pcm->streams[cidx].dev);
		if (err < 0) {
			list_del_init(&pcm->list);
			goto unlock;
		}

		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
			snd_pcm_timer_init(substream);
	}

	pcm_call_notify(pcm, n_register);

 unlock:
	mutex_unlock(&register_mutex);
	return err;
}

然后调用函数snd_register_device完成最终的注册.

/**
 * snd_register_device - Register the ALSA device file for the card
 * @type: the device type, SNDRV_DEVICE_TYPE_XXX  //设备类型
 * @card: the card instance   //声卡实例
 * @dev: the device index     
 * @f_ops: the file operations  //文件操作
 * @private_data: user pointer for f_ops->open()
 * @device: the device to register
 *
 * Registers an ALSA device file for the given card.
 * The operators have to be set in reg parameter.
 *
 * Return: Zero if successful, or a negative error code on failure.
 */
int snd_register_device(int type, struct snd_card *card, int dev,
			const struct file_operations *f_ops,
			void *private_data, struct device *device)
{
	int minor;
	int err = 0;
	struct snd_minor *preg;  //建立次设备

	if (snd_BUG_ON(!device))
		return -EINVAL;
   /*给次设备赋值*/
	preg = kmalloc(sizeof *preg, GFP_KERNEL);
	if (preg == NULL)
		return -ENOMEM;
	preg->type = type;
	preg->card = card ? card->number : -1;
	preg->device = dev;
	preg->f_ops = f_ops;   //获取文件操作结构体
	preg->private_data = private_data;
	preg->card_ptr = card;
	mutex_lock(&sound_mutex);
	minor = snd_find_free_minor(type, card, dev);
	if (minor < 0) {
		err = minor;
		goto error;
	}

	preg->dev = device;
	device->devt = MKDEV(major, minor); //通过主次设备号来生成dev_t
	err = device_add(device);
	if (err < 0)
		goto error;

	snd_minors[minor] = preg;
 error:
	mutex_unlock(&sound_mutex);
	if (err < 0)
		kfree(preg);
	return err;
}
EXPORT_SYMBOL(snd_register_device);

主要作用:

== (1)获取声卡次设备snd_minor ,并将其赋值给全局声卡次设备变量snd_minor中 ==

== (2)获取次设备pcm的文件操作结构体,供用户层使用的api回调函数。snd_pcm_f_ops作为snd_register_device的参数被传入,并被记录在snd_minors[minor]中的字段f_ops中.最后创建设备节点。创建节点之后我们就能在/dev目录下查看到相应的设备文件 ==

snd_pcm_dev_register 调用 snd_register_device 注册PCM设备参数ops为 == snd_pcm_f_ops ==
5.1.2 pcm设备的文件操作结构体
回调函数,供用户层使用的api。分为playback和capature,即播放设备和录音设备。

/*
 *  Register section
 */

const struct file_operations snd_pcm_f_ops[2] = {
	{
		.owner =		THIS_MODULE,
		.write =		snd_pcm_write,
		.write_iter =		snd_pcm_writev,
		.open =			snd_pcm_playback_open,  //播放
		.release =		snd_pcm_release,
		.llseek =		no_llseek,
		.poll =			snd_pcm_poll,
		.unlocked_ioctl =	snd_pcm_ioctl,
		.compat_ioctl = 	snd_pcm_ioctl_compat,
		.mmap =			snd_pcm_mmap,
		.fasync =		snd_pcm_fasync,
		.get_unmapped_area =	snd_pcm_get_unmapped_area,
	},
	{
		.owner =		THIS_MODULE,
		.read =			snd_pcm_read,
		.read_iter =		snd_pcm_readv,
		.open =			snd_pcm_capture_open,  //录音
		.release =		snd_pcm_release,
		.llseek =		no_llseek,
		.poll =			snd_pcm_poll,
		.unlocked_ioctl =	snd_pcm_ioctl,
		.compat_ioctl = 	snd_pcm_ioctl_compat,
		.mmap =			snd_pcm_mmap,
		.fasync =		snd_pcm_fasync,
		.get_unmapped_area =	snd_pcm_get_unmapped_area,
	}
};

总结一下:
注册声卡,在这个阶段会遍历声卡下的所有逻辑设备,并且调用各设备的注册回调函数,对于pcm,就是第二步提到的snd_pcm_dev_register函数,该回调函数建立了和用户空间应用程序(alsa-lib)通信所用的设备文件节点:/dev/snd/pcmCxxDxxp和/dev/snd/pcmCxxDxxc
注:一共有两个关键的结构体:
(1)设备操作结构体snd_device_ops ,在声卡创建时注册回调函数,主要是设备注册回调函数snd_pcm_dev_register,在声卡注册时会调用其注册pcm设备
(2)文件操作结构体struct file_operations snd_pcm_f_ops,在pcm设备注册函数snd_pcm_dev_register中,加入全局变量snd_minors数组中,就是告诉声卡,供用户调用的接口。用户层操作时会获取到数组snd_minors的下标,就可以找到相应的次设备是control或者pcm,操作对用文件操作结构体中的函数。
以上api的调用关系:

在这里插入图片描述
在这里插入图片描述
以上代码我们可以看出,对于一个pcm设备,可以生成两个设备文件,一个用于playback,一个用于capture,代码中也确定了他们的命名规则:

(1)playback – pcmCxDxp,通常系统中只有一各声卡和一个pcm,它就是pcmC0D0p

(2)capture – pcmCxDxc,通常系统中只有一各声卡和一个pcm,它就是pcmC0D0c
== playback capture 都有对应的ops ==

6 、pcm设备的open

以从应用程序到驱动层pcm为例一步一步讲解。

6.1 声卡字符设备的注册
在sound/core/sound.c中有alsa_sound_init()函数,定义如下:

static int __init alsa_sound_init(void)
{
	snd_major = major;
	snd_ecards_limit = cards_limit;
	if (register_chrdev(major, "alsa", &snd_fops)) {
		pr_err("ALSA core: unable to register native major device number %d\n", major);
		return -EIO;
	}
	if (snd_info_init() < 0) {
		unregister_chrdev(major, "alsa");
		return -ENOMEM;
	}

#ifdef CONFIG_SND_DEBUG
	sound_debugfs_root = debugfs_create_dir("sound", NULL);
#endif
#ifndef MODULE
	pr_info("Advanced Linux Sound Architecture Driver Initialized.\n");
#endif
	return 0;
}

我们使用register_chrdev函数注册alsa 设备 使用了major作为主设备号
== major与之前创建pcm设备是device_create时的major是同一个,这样的结果是,当应用程序open设备文件/dev/snd/pcmCxDxp时,会进入snd_fops的open回调函数。==

6.2 打开pcm设备
从上一节中我们得知,open一个pcm设备时,将会调用snd_fops的open回调函数,我们先看看snd_fops的定义:

static const struct file_operations snd_fops =
{
	.owner =	THIS_MODULE,
	.open =		snd_open,
	.llseek =	noop_llseek,
};

跟入snd_open函数,它首先从inode中取出此设备号,然后以次设备号为索引,从snd_minors全局数组中取出当初注册pcm设备时填充的snd_minor结构(参看第一节中的snd_open函数内容),

然后从snd_minor结构中取出pcm设备的f_ops,并且把file->f_op替换为pcm设备的f_ops,紧接着直接调用pcm设备的f_ops->open(),然后返回.因为file->f_op已经被替换,以后,应用程序的所有read/write/ioctl调用都会进入pcm设备自己的回调函数中,也就是5.1.2节中提到的snd_pcm_f_ops结构中定义的回调。

//inode eg:/dev/snd/pcmCxDxp
static int snd_open(struct inode *inode, struct file *file)
{
	unsigned int minor = iminor(inode); //iminor 通过节点名称返回次设备号,同类型函数imajor返回主设备号
	struct snd_minor *mptr = NULL;
	const struct file_operations *new_fops;
	int err = 0;

	if (minor >= ARRAY_SIZE(snd_minors))
		return -ENODEV;
	mutex_lock(&sound_mutex);
	mptr = snd_minors[minor]; //获取到具体的声卡设备,即次设备比如control/pcm设备等
	if (mptr == NULL) {
		mptr = autoload_device(minor);
		if (!mptr) {
			mutex_unlock(&sound_mutex);
			return -ENODEV;
		}
	}
	new_fops = fops_get(mptr->f_ops); //获取PCM设备/dev/snd/pcmCxDxp 的f_ops文件操作结构体
	mutex_unlock(&sound_mutex);
	if (!new_fops)
		return -ENODEV;
	replace_fops(file, new_fops); //用/dev/snd/pcmCxDxp 的f_ops文件操作结构体 替换

	if (file->f_op->open)
		err = file->f_op->open(inode, file);  //执行/dev/snd/pcmCxDxp 的f_ops—>open函数
	return err;
}

6.3 总结以上过程

下面的序列图表示了应用程序如何最终调用到snd_pcm_f_ops结构中的回调函数:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值