1. PCM是什么
-
playback 如何把用户空间的应用程序发过来的PCM数据,转化为人耳可以辨别的模拟音频
-
capture 把mic拾取到得模拟信号,经过采样、量化,转换为PCM信号送回给用户空间的应用程序
2. alsa-driver中的PCM中间层
-
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
-
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
-
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 */
-
};
-
static struct snd_minor *snd_minors[256];
-
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);
-
......
-
}
-
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;
-
mutex_lock(&sound_mutex);
-
#ifdef CONFIG_SND_DYNAMIC_MINORS
-
minor = snd_find_free_minor();
-
#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);
-
if (IS_ERR(preg->dev)) {
-
snd_minors[minor] = NULL;
-
mutex_unlock(&sound_mutex);
-
minor = PTR_ERR(preg->dev);
-
kfree(preg);
-
return minor;
-
}
-
-
mutex_unlock(&sound_mutex);
-
return 0;
-
}
-
首先,分配并初始化一个snd_minor结构中的各字段
-
type:SNDRV_DEVICE_TYPE_PCM_PLAYBACK/SNDRV_DEVICE_TYPE_PCM_CAPTURE
-
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 设备文件的建立
-
static int snd_pcm_dev_register(struct snd_device *device)
-
{
-
int cidx, err;
-
char str[16];
-
struct snd_pcm *pcm;
-
struct device *dev;
-
-
pcm = device->device_data;
-
......
-
for (cidx = 0; cidx < 2; cidx++) {
-
......
-
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);
-
......
-
}
-
......
-
}
-
playback -- pcmCxDxp,通常系统中只有一各声卡和一个pcm,它就是pcmC0D0p
-
capture -- pcmCxDxc,通常系统中只有一各声卡和一个pcm,它就是pcmC0D0c
-
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_minors[minor] = preg;
-
preg->dev = device_create(sound_class, device, MKDEV(major, minor),
-
private_data, "%s", name);
4.3 层层深入,从应用程序到驱动层pcm
4.3.1 字符设备注册
-
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;
-
}
4.3.2 打开pcm设备
-
static const struct file_operations snd_fops =
-
{
-
.owner = THIS_MODULE,
-
.open = snd_open
-
};
-
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];
-
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);
-
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) {
-
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;
-
}