Linux LASA声卡驱动之三:PCM设备的创建

本文深入探讨了PCM(脉冲编码调制)技术,包括其基本原理、在ALSA驱动中的实现,以及如何在Linux系统中创建和使用PCM设备。详细介绍了PCM信号的采样频率和量化精度,以及在嵌入式系统中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.PCM是什么

PCM是英文Pulse-code modulation的缩写,中文译名是脉冲编码调制。PCM就是把声音从模拟转换成数字信号的一种技术,原理很简单,就是利用一个固定的频率对模拟信号进行采样,采用后的信号在波形上看就像一连串连续的幅值不一的脉冲,把这些脉冲的幅值按一定的精度进行量化,这些量化后的数值被连续地输出传输处理或记录到存储介质中,所有这些组成了数字音频的产生过程。

PCM信号的两个重要指标是采样频率和量化精度,目前CD音频的采样频率通常为44100HZ,量化精度是16bit,通常播放音乐时,应用程序从存储介质中读取音频数据,经过解码后,最终送到音频驱动程序的PCM数据,反过来,在录音时,音频驱动不停的把采样所得数据送回给应用程序,由应用程序完成压缩,存储等任务,因此,音频驱动的两个核心任务就是:

playback 如何把用户空间的应用程序发过来的PCM数据,转化为人耳可以分辨的模拟音频

capture  把mic拾取到的模拟信号,经过采样、量化转换为PCM信号送会给用户空间的应用程序。

2.alsa-driver的中的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组成。

在嵌入式系统中,通常不会像上图中那么复杂,大多数情况下是一个声卡,一个pcm实例,pcm下面有一个playback和capture stream,playback 和capture下面各自有一个substream。

下图列出了PCM中间层几个重要的结构体,从uml角度看这几个结构体之间的关系。

snd_pcm是挂在snd_card下面的一个snd_device

snd_pcm中的字段:streams[2],在数组中的两个元素指向两个snd_pcm_str结构,分别代表playback stream 和capture stream

snd_pcm_str中的substream字段,指向snd_pcm_substream结构。

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

3.新建一个PCM

alsa-driver的中间层已经为我们提供了新建pcm的api:

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);
}

参数device表示目前创建的是该声卡下的第几个pcm,第一个pcm设备从0开始。

参数playback_count表示该PCM将会有几个playback substream。

参数capture_count表示该PCM将会有几个capture substream、

另一个用于设置PCM操作函数接口的api:

void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, 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可以用下面一张新建pcm的调用图进行描述:

snd_card_create:pcm是声卡下的一个设备,因此第一步要创建一个声卡

snd_pcm_new:调用该api创建一个pcm,在该api中会做以下事情

    如果有,建立playback stream,相应的substream也同时建立。

    如果有,建立capture stream,相应的substream也同时建立。

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

snd_pcm_set_ops 设置操作该pcm的控制/操作接口函数,参数中的snd_pcm_ops结构中的函数通常就是我们驱动要实现的函数。

snd_card_register注册声卡,在这个阶段会遍历声卡下的所有逻辑设备,并且调用各设备的注册回调函数,对于pcm就是第二部提到的snd_pcm_dev_register函数,该回调函数建立了和用户空间应用程序(alsa-lib)通信所用的设备文件节点:/dev/snd/pcmCxxDxxp和/dev/snd/pcmCxxDxxc

4.设备文件节点的建立(dev/snd/pcmCxxDxxp,pcmCxxDxxc)

4.1 struct snd_minor

每个snd_minor结构体保存了声卡下某个逻辑设备的上下文信息,他在逻辑设备建立阶段被填充,在逻辑设备被使用时就可以从该结构中得到相应的信息,pcm设备也不例外,也需要使用该结构体,该结构体在include/sound/core.h中定义。

struct snd_minor {
	int type;			/* SNDRV_DEVICE_TYPE_XXX */
	int card;			/* card number */
	int device;			/* device number */
	const struct file_operations *f_ops;	/* file operations */
	void *private_data;		/* private data for f_ops->open */
	struct device *dev;		/* device for sysfs */
	struct snd_card *card_ptr;	/* assigned card instance */
};

在sound/sound.c中定义了一个snd_minor指针的全局数组:

static struct snd_minor *snd_minors[SNDRV_OS_MINORS];

前面说过,在声卡的注册阶段(snd_card_register),会调用pcm的回调函数snd_pcm_dev_register(),这个函数里会调用函数snd_register_device_for_dev():

static int snd_pcm_dev_register(struct snd_device *device)
{
        ......
		/* register pcm */
		err = snd_register_device_for_dev(devtype, pcm->card,
						  pcm->device,
						  &snd_pcm_f_ops[cidx],
						  pcm, str, dev);
        ......
}

在进入snd_pcm_dev_register()

int snd_register_device_for_dev(int type, struct snd_card *card, int dev,
				const struct file_operations *f_ops,
				void *private_data,
				const char *name, struct device *device)
{
	int minor;
	struct snd_minor *preg;

	if (snd_BUG_ON(!name))
		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);
#ifdef CONFIG_SND_DYNAMIC_MINORS
	minor = snd_find_free_minor(type);
#else
	minor = snd_kernel_minor(type, card, dev);
	if (minor >= 0 && snd_minors[minor])
		minor = -EBUSY;
#endif
	if (minor < 0) {
		mutex_unlock(&sound_mutex);
		kfree(preg);
		return minor;
	}
	snd_minors[minor] = preg;
	preg->dev = device_create(sound_class, device, MKDEV(major, minor),
				  private_data, "%s", name);


	mutex_unlock(&sound_mutex);
	return 0;
}

首先,分配并初始化一个snd_minor结构中的各字段

        type:SNDRV_DEVICE_TYPE_PCM_CAPTURE 和 SNDRV_DEVICE_TYPE_PCM_PLAYBACK

        card:card的编号

        device:pcm实例的编号,大多数情况为0

        f_ops:snd_pcm_f_ops

         private_data:指向该pcm的实例

根据type,card和pcm编号,确定数组的索引值minor,minor也作为pcm设备的次设备号。

把该snd_minor结构的地址放入全局数组的snd_minors[minor]中

最后调用device_create创建设备节点。

4.2设备文件的建立

在4.1节的最后,设备文件已经建立,不过4.1节的重点在于snd_minors数组的赋值过程,在本节中,我们把重点放在 设备文件中。回到pcm回调函数snd_pcm_dev_register()中:

static int snd_pcm_dev_register(struct snd_device *device)
{
	int cidx, err;
	struct snd_pcm_substream *substream;
	struct snd_pcm_notify *notify;
	char str[16];
	struct snd_pcm *pcm;
	struct device *dev;

	pcm = device->device_data;
	mutex_lock(&register_mutex);
	err = snd_pcm_add(pcm);

	for (cidx = 0; cidx < 2; cidx++) {
		int devtype = -1;
		if (pcm->streams[cidx].substream == NULL || pcm->internal)
			continue;
		switch (cidx) {
		case SNDRV_PCM_STREAM_PLAYBACK:
			sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);
			devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
			break;
		case SNDRV_PCM_STREAM_CAPTURE:
			sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);
			devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
			break;
		}
		/* device pointer to use, pcm->dev takes precedence if
		 * it is assigned, otherwise fall back to card's device
		 * if possible */
		dev = pcm->dev;
		if (!dev)
			dev = snd_card_get_device_link(pcm->card);
		/* register pcm */
		err = snd_register_device_for_dev(devtype, pcm->card,
						  pcm->device,
						  &snd_pcm_f_ops[cidx],
						  pcm, str, dev);

		snd_add_device_sysfs_file(devtype, pcm->card, pcm->device,
					  &pcm_attrs);
		for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
			snd_pcm_timer_init(substream);
	}


	return 0;
}

以上代码可以看出,对于一个pcm设备,可以生产两个设备文件,一个用于playback,一个用于capture,代码中确定了他们的命名规则:

playback------pcmCxDxp,通常系统中只有一个声卡和一个pcm,他就是pcmC0D0p

capture------pcmCxDxc,通常系统中只有一个声卡和一个pcm,他就是pcmC0D0c

snd_pcm_f_ops

snd_pcm_f_ops是一个标准的文件系统file_operations结构数组,它的定义在sound/core/pcm_native.c中:

const struct file_operations snd_pcm_f_ops[2] = {
	{
		.owner =		THIS_MODULE,
		.write =		snd_pcm_write,
		.aio_write =		snd_pcm_aio_write,
		.open =			snd_pcm_playback_open,
		.release =		snd_pcm_release,
		.llseek =		no_llseek,
		.poll =			snd_pcm_playback_poll,
		.unlocked_ioctl =	snd_pcm_playback_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,
		.aio_read =		snd_pcm_aio_read,
		.open =			snd_pcm_capture_open,
		.release =		snd_pcm_release,
		.llseek =		no_llseek,
		.poll =			snd_pcm_capture_poll,
		.unlocked_ioctl =	snd_pcm_capture_ioctl,
		.compat_ioctl = 	snd_pcm_ioctl_compat,
		.mmap =			snd_pcm_mmap,
		.fasync =		snd_pcm_fasync,
		.get_unmapped_area =	snd_pcm_get_unmapped_area,
	}
};

snd_pcm_f_ops作为snd_register_device_for_dev的参数传入,并被记录在snd_minors[minor]中的字段f_ops中,最后在snd_register_Device_for_dev中创建设备节点。

	snd_minors[minor] = preg;
	preg->dev = device_create(sound_class, device, MKDEV(major, minor),

4.3层层深入,从应用程序到驱动层pcm

4.3.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)) {
		snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
		return -EIO;
	}
	if (snd_info_init() < 0) {
		unregister_chrdev(major, "alsa");
		return -ENOMEM;
	}
	snd_info_minor_register();

	return 0;
}

register_chardev中的参数major与之前创建的pcm设备是device_create时的major是同一个,这样的结果是,当应用程序open设备文件/dev/snd/pcmCxDxp时,会进入snd_fops的open回调函数,我们将在下一节中讲述open的过程。

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

static int snd_open(struct inode *inode, struct file *file)
{
	unsigned int minor = iminor(inode); //从inode中取出次设备号
	struct snd_minor *mptr = NULL;
	const struct file_operations *old_fops;
	int err = 0;

	if (minor >= ARRAY_SIZE(snd_minors))
		return -ENODEV;

	mptr = snd_minors[minor];       //从snd_minors中取出pcm的f_ops
	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;
	}


	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;
}

下面的序列图展示了应用程序如何最终调用到snd_pcm_f_ops结构中的回调函数:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值