Linux音频驱动-PCM设备

概述

1.  什么是pcm?
pcm(Pulse-code modulation)脉冲编码调制,是将模拟信号转化为数字信号的一种方法。声音的转化的过程为,先对连续的模拟信号按照固定频率周期性采样,将采样到的数据按照一定的精度进行量化,量化后的信号和采样后的信号差值叫做量化误差,将量化后的数据进行最后的编码存储,最终模拟信号变化为数字信号。

2. pcm的两个重要属性
    a.  采样率:        单位时间内采样的次数,采样频率越高越高,
    b.  采样位数:    一个采样信号的位数,也是对采样精度的变现。

对于人类而言,能接受声音的频率范围是20Hz-20KHz, 所以采样的频率44.1KHz 以及16bit的采样位数就可以有很好的保真能力(CD格式的采样率和采样位数)。


                                                              图1-1  声音的录音和播放过程

数据结构

在ALSA架构下,pcm也被称为设备,所谓的逻辑设备。在linux系统中使用snd_pcm结构表示一个pcm设备。
[cpp]  view plain  copy
  1. struct snd_pcm {  
  2.     struct snd_card *card;  
  3.     struct list_head list;  
  4.     int device; /* device number */  
  5.     unsigned int info_flags;  
  6.     unsigned short dev_class;  
  7.     unsigned short dev_subclass;  
  8.     char id[64];  
  9.     char name[80];  
  10.     struct snd_pcm_str streams[2];  
  11.     struct mutex open_mutex;  
  12.     wait_queue_head_t open_wait;  
  13.     void *private_data;  
  14.     void (*private_free) (struct snd_pcm *pcm);  
  15.     struct device *dev; /* actual hw device this belongs to */  
  16.     bool internal; /* pcm is for internal use only */  
  17.     bool nonatomic; /* whole PCM operations are in non-atomic context */  
  18. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)  
  19.     struct snd_pcm_oss oss;  
  20. #endif  
  21. };  
.card:         此pcm设备所属的card。
.list:           用于将pcm设备链接起来,最终所有的pcm设备会放入snd_pcm_devices链表中。
.device:      该pcm的索引号。
.id:             该pcm的标识。
.streams:   指向pcm的capture和playback stream,通常0代表playback,1代表capture。

通常一个pcm下会有两个stream, 分别为capture stream和playback stream,在每个stream下又会存在多个substream。
linux系统中使用snd_pcm_str定义stream, 使用snd_pcm_substream定义substream。
[cpp]  view plain  copy
  1. struct snd_pcm_str {  
  2.     int stream;             /* stream (direction) */  
  3.     struct snd_pcm *pcm;  
  4.     /* -- substreams -- */  
  5.     unsigned int substream_count;  
  6.     unsigned int substream_opened;  
  7.     struct snd_pcm_substream *substream;  
  8. };  
.stream:  当前stream的方向,capture or playback。
.pcm:      所属的pcm。
.substream_count:  该stream下substream的个数。
.substream_opened:  该stream下open的substream个数。
.substream:  该stream下的substream.

[cpp]  view plain  copy
  1. struct snd_pcm_substream {  
  2.     struct snd_pcm *pcm;  
  3.     struct snd_pcm_str *pstr;  
  4.     void *private_data;     /* copied from pcm->private_data */  
  5.     int number;  
  6.     char name[32];          /* substream name */  
  7.     int stream;         /* stream (direction) */  
  8.     struct pm_qos_request latency_pm_qos_req; /* pm_qos request */  
  9.     size_t buffer_bytes_max;    /* limit ring buffer size */  
  10.     struct snd_dma_buffer dma_buffer;  
  11.     size_t dma_max;  
  12.     /* -- hardware operations -- */  
  13.     const struct snd_pcm_ops *ops;  
  14.     /* -- runtime information -- */  
  15.     struct snd_pcm_runtime *runtime;  
  16.         /* -- timer section -- */  
  17.     struct snd_timer *timer;        /* timer */  
  18.     unsigned timer_running: 1;  /* time is running */  
  19.     /* -- next substream -- */  
  20.     struct snd_pcm_substream *next;  
  21.     /* -- linked substreams -- */  
  22.     struct list_head link_list; /* linked list member */  
  23.     struct snd_pcm_group self_group;    /* fake group for non linked substream (with substream lock inside) */  
  24.     struct snd_pcm_group *group;        /* pointer to current group */  
  25.     /* -- assigned files -- */  
  26.     void *file;  
  27.     int ref_count;  
  28.     atomic_t mmap_count;  
  29.     unsigned int f_flags;  
  30.     void (*pcm_release)(struct snd_pcm_substream *);  
  31.     struct pid *pid;  
  32.     /* misc flags */  
  33.     unsigned int hw_opened: 1;  
  34. };  
.pcm:       所属的pcm。
.pstr:       所属的stream。
.id:           代表的该stream下第几个substream,也就是序号。
.stream:  该substream的方向流,是palyback or capture。
.name:     该substrem的名字。
.ops:        硬件操作函数集合。
.runtime:   运行时的pcm的一些信息。
.next:        用于链接下一个sub stream。

下图是对这几个结构体之间的简单表述。



pcm设备的创建

创建一个pcm设备的实例,使用snd_pcm_new函数。
[cpp]  view plain  copy
  1. /** 
  2.  * snd_pcm_new - create a new PCM instance 
  3.  * @card: the card instance 
  4.  * @id: the id string 
  5.  * @device: the device index (zero based) 
  6.  * @playback_count: the number of substreams for playback 
  7.  * @capture_count: the number of substreams for capture 
  8.  * @rpcm: the pointer to store the new pcm instance 
  9.  * 
  10.  * Creates a new PCM instance. 
  11.  * 
  12.  * The pcm operators have to be set afterwards to the new instance 
  13.  * via snd_pcm_set_ops(). 
  14.  * 
  15.  * Return: Zero if successful, or a negative error code on failure. 
  16.  */  
  17. int snd_pcm_new(struct snd_card *card, const char *id, int device,  
  18.         int playback_count, int capture_count, struct snd_pcm **rpcm)  
  19. {  
  20.     return _snd_pcm_new(card, id, device, playback_count, capture_count,  
  21.             false, rpcm);  
  22. }  
此函数会传入六个参数,其中该函数的注释写的很清楚,不做过多解释。函数最终会返回rpcm参数。
[cpp]  view plain  copy
  1. static int _snd_pcm_new(struct snd_card *card, const char *id, int device,  
  2.         int playback_count, int capture_count, bool internal,  
  3.         struct snd_pcm **rpcm)  
  4. {  
  5.     struct snd_pcm *pcm;  
  6.     int err;  
  7.     static struct snd_device_ops ops = {  
  8.         .dev_free = snd_pcm_dev_free,  
  9.         .dev_register = snd_pcm_dev_register,  
  10.         .dev_disconnect = snd_pcm_dev_disconnect,  
  11.     };  
  12.   
  13.     if (snd_BUG_ON(!card))  
  14.         return -ENXIO;  
  15.     if (rpcm)  
  16.         *rpcm = NULL;  
  17.     pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);  
  18.     if (pcm == NULL) {  
  19.         dev_err(card->dev, "Cannot allocate PCM\n");  
  20.         return -ENOMEM;  
  21.     }  
  22.     pcm->card = card;  
  23.     pcm->device = device;  
  24.     pcm->internal = internal;  
  25.     if (id)  
  26.         strlcpy(pcm->id, id, sizeof(pcm->id));  
  27.     if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {  
  28.         snd_pcm_free(pcm);  
  29.         return err;  
  30.     }  
  31.     if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {  
  32.         snd_pcm_free(pcm);  
  33.         return err;  
  34.     }  
  35.     mutex_init(&pcm->open_mutex);  
  36.     init_waitqueue_head(&pcm->open_wait);  
  37.     if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {  
  38.         snd_pcm_free(pcm);  
  39.         return err;  
  40.     }  
  41.     if (rpcm)  
  42.         *rpcm = pcm;  
  43.     return 0;  
  44. }  
1.  分配一个snd_pcm结构体。
2.  根据传递进来的参数设置card, device, internal, id。
3.  分别创建palyback & capture stream。
4.  调用snd_device_new接口创建pcm设备。

调用snd_pcm_new_stream创建一个stream
[cpp]  view plain  copy
  1. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)  
  2. {  
  3.     int idx, err;  
  4.     struct snd_pcm_str *pstr = &pcm->streams[stream];  
  5.     struct snd_pcm_substream *substream, *prev;  
  6.   
  7. #if IS_ENABLED(CONFIG_SND_PCM_OSS)  
  8.     mutex_init(&pstr->oss.setup_mutex);  
  9. #endif  
  10.     pstr->stream = stream;  
  11.     pstr->pcm = pcm;  
  12.     pstr->substream_count = substream_count;  
  13.     if (substream_count > 0 && !pcm->internal) {  
  14.         err = snd_pcm_stream_proc_init(pstr);  
  15.         if (err < 0) {  
  16.             pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");  
  17.             return err;  
  18.         }  
  19.     }  
  20.     prev = NULL;  
  21.     for (idx = 0, prev = NULL; idx < substream_count; idx++) {  
  22.         substream = kzalloc(sizeof(*substream), GFP_KERNEL);  
  23.         if (substream == NULL) {  
  24.             pcm_err(pcm, "Cannot allocate PCM substream\n");  
  25.             return -ENOMEM;  
  26.         }  
  27.         substream->pcm = pcm;  
  28.         substream->pstr = pstr;  
  29.         substream->number = idx;  
  30.         substream->stream = stream;  
  31.         sprintf(substream->name, "subdevice #%i", idx);  
  32.         substream->buffer_bytes_max = UINT_MAX;  
  33.         if (prev == NULL)  
  34.             pstr->substream = substream;  
  35.         else  
  36.             prev->next = substream;  
  37.   
  38.         if (!pcm->internal) {  
  39.             err = snd_pcm_substream_proc_init(substream);  
  40.             if (err < 0) {  
  41.                 pcm_err(pcm,  
  42.                     "Error in snd_pcm_stream_proc_init\n");  
  43.                 if (prev == NULL)  
  44.                     pstr->substream = NULL;  
  45.                 else  
  46.                     prev->next = NULL;  
  47.                 kfree(substream);  
  48.                 return err;  
  49.             }  
  50.         }  
  51.         substream->group = &substream->self_group;  
  52.         spin_lock_init(&substream->self_group.lock);  
  53.         mutex_init(&substream->self_group.mutex);  
  54.         INIT_LIST_HEAD(&substream->self_group.substreams);  
  55.         list_add_tail(&substream->link_list, &substream->self_group.substreams);  
  56.         atomic_set(&substream->mmap_count, 0);  
  57.         prev = substream;  
  58.     }  
  59.     return 0;  
  60. }             
1.   根据传递进来的参数,设置pcm的stream, pcm, substream_count的值。
2.   在proc下创建pcm相关目录信息。会调用snd_pcm_stream_proc_init函数,根据stream的类型创建pcm0p/pcm0c文件夹,然后会在此文件夹下创建info文件。info文件的类型会通过snd_pcm_stream_proc_info_read函数获得。代表就不贴出来了。:(
[cpp]  view plain  copy
  1. root@test:/proc/asound/card0/pcm0c$ cat info   
  2. card: 0  
  3. device: 0  
  4. subdevice: 0  
  5. stream: CAPTURE  
  6. id: ALC662 rev1 Analog  
  7. name: ALC662 rev1 Analog  
  8. subname: subdevice #0  
  9. class: 0  
  10. subclass: 0  
  11. subdevices_count: 1  
  12. subdevices_avail: 1  
3.   会根据substrem_count的个数,进行for循环操作。
4.   分配一个substream结构,设置必要的参数,如:  pcm,  pstr,  number,  stream,  name等。
5.   调用snd_pcm_substream_proc_init函数,创建sub0目录,然后在此目录下创建info, hw_params, sw_params,status等文件。
6.   将所有的substream会通过linklist链表保存,同时如果有多个substream会通过next指针相连。

至此,pcm设备就全部创建完成,创建完成后会形成如下的逻辑试图。

大体上就是一棵树,根节点是card0, 然后子节点是pcm设备,pcm设备分为capture & playback stream, 然后在stream下又分为substrem。

PCM硬件操作函数集设置

实例化一个pcm设备之后,还需要通过snd_pcm_set_ops函数设置该硬件的操作集合。
[cpp]  view plain  copy
  1. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,  
  2.              const struct snd_pcm_ops *ops)  
  3. {  
  4.     struct snd_pcm_str *stream = &pcm->streams[direction];  
  5.     struct snd_pcm_substream *substream;  
  6.       
  7.     for (substream = stream->substream; substream != NULL; substream = substream->next)  
  8.         substream->ops = ops;  
  9. }  
该函数会根据当前stream的方向/类型,设置该硬件对应的snd_pcm_ops操作集合。

整个流程梳理


PCM设备节点创建

当调用snd_card_register的时候,就会依次调用card列表下每个设备的dev_register回调函数,对pcm设备来说就是在_snd_pcm_new函数中的
[cpp]  view plain  copy
  1. static struct snd_device_ops ops = {  
  2.     .dev_free = snd_pcm_dev_free,  
  3.     .dev_register = snd_pcm_dev_register,  
  4.     .dev_disconnect = snd_pcm_dev_disconnect,  
  5. };  
此时会调用到snd_pcm_dev_register回调处理函数。
[cpp]  view plain  copy
  1. static int snd_pcm_dev_register(struct snd_device *device)  
  2. {  
  3.     int cidx, err;  
  4.     struct snd_pcm_substream *substream;  
  5.     struct snd_pcm_notify *notify;  
  6.     char str[16];  
  7.     struct snd_pcm *pcm;  
  8.     struct device *dev;  
  9.   
  10.     if (snd_BUG_ON(!device || !device->device_data))  
  11.         return -ENXIO;  
  12.     pcm = device->device_data;  
  13.     mutex_lock(&register_mutex);  
  14.     err = snd_pcm_add(pcm);  
  15.     if (err) {  
  16.         mutex_unlock(&register_mutex);  
  17.         return err;  
  18.     }  
  19.     for (cidx = 0; cidx < 2; cidx++) {  
  20.         int devtype = -1;  
  21.         if (pcm->streams[cidx].substream == NULL || pcm->internal)  
  22.             continue;  
  23.         switch (cidx) {  
  24.         case SNDRV_PCM_STREAM_PLAYBACK:  
  25.             sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);  
  26.             devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;  
  27.             break;  
  28.         case SNDRV_PCM_STREAM_CAPTURE:  
  29.             sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);  
  30.             devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;  
  31.             break;  
  32.         }  
  33.         /* device pointer to use, pcm->dev takes precedence if 
  34.          * it is assigned, otherwise fall back to card's device 
  35.          * if possible */  
  36.         dev = pcm->dev;  
  37.         if (!dev)  
  38.             dev = snd_card_get_device_link(pcm->card);  
  39.         /* register pcm */  
  40.         err = snd_register_device_for_dev(devtype, pcm->card,  
  41.                           pcm->device,  
  42.                           &snd_pcm_f_ops[cidx],  
  43.                           pcm, str, dev);  
  44.         if (err < 0) {  
  45.             list_del(&pcm->list);  
  46.             mutex_unlock(&register_mutex);  
  47.             return err;  
  48.         }  
  49.   
  50.         dev = snd_get_device(devtype, pcm->card, pcm->device);  
  51.         if (dev) {  
  52.             err = sysfs_create_groups(&dev->kobj,  
  53.                           pcm_dev_attr_groups);  
  54.             if (err < 0)  
  55.                 dev_warn(dev,  
  56.                      "pcm %d:%d: cannot create sysfs groups\n",  
  57.                      pcm->card->number, pcm->device);  
  58.             put_device(dev);  
  59.         }  
  60.   
  61.         for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)  
  62.             snd_pcm_timer_init(substream);  
  63.     }  
  64.   
  65.     list_for_each_entry(notify, &snd_pcm_notify_list, list)  
  66.         notify->n_register(pcm);  
  67.   
  68.     mutex_unlock(&register_mutex);  
  69.     return 0;  
  70. }  
1.   合法性判断,对pcm设备来说,snd_device->device_data存放的是当前的pcm指针。
2.    会调用snd_pcm_add此函数,判断此pcm设备是存在snd_pcm_devices链表中存在,存在就返回错误,不存在就添加。
3.    设置当前pcm设备name, 以及具体的pcm设备类型,PCM_CAPTURE  or PCM_PLAYBACK。
4.    调用snd_register_device_for_dev添加pcm设备到系统中。
5.    调用snd_get_device此函数返回当前注册的pcm设备,然后设置该pcm的属性。
6.    调用snd_pcm_timer_init函数,进行pcm定时器的初始化。

在继续分析snd_register_device_for_dev函数之前需要先介绍一个结构体。struct snd_minor。
[cpp]  view plain  copy
  1. struct snd_minor {  
  2.     int type;           /* SNDRV_DEVICE_TYPE_XXX */  
  3.     int card;           /* card number */  
  4.     int device;         /* device number */  
  5.     const struct file_operations *f_ops;    /* file operations */  
  6.     void *private_data;     /* private data for f_ops->open */  
  7.     struct device *dev;     /* device for sysfs */  
  8.     struct snd_card *card_ptr;  /* assigned card instance */  
  9. };  
.type:  设备类型,比如是pcm, control, timer等设备。
.card_number:  所属的card。
.device:  当前设备类型下的设备编号。
.f_ops:  具体设备的文件操作集合。
.private_data:  open函数的私有数据。
.card_ptr:  所属的card。

此结构体是用来保存当前设备的上下文信息,该card下所有逻辑设备都存在此结构。

[cpp]  view plain  copy
  1. int snd_register_device_for_dev(int type, struct snd_card *card, int dev,  
  2.                 const struct file_operations *f_ops,  
  3.                 void *private_data,  
  4.                 const char *name, struct device *device)  
  5. {  
  6.     int minor;  
  7.     struct snd_minor *preg;  
  8.   
  9.     if (snd_BUG_ON(!name))  
  10.         return -EINVAL;  
  11.     preg = kmalloc(sizeof *preg, GFP_KERNEL);  
  12.     if (preg == NULL)  
  13.         return -ENOMEM;  
  14.     preg->type = type;  
  15.     preg->card = card ? card->number : -1;  
  16.     preg->device = dev;  
  17.     preg->f_ops = f_ops;  
  18.     preg->private_data = private_data;  
  19.     preg->card_ptr = card;  
  20.     mutex_lock(&sound_mutex);  
  21. #ifdef CONFIG_SND_DYNAMIC_MINORS  
  22.     minor = snd_find_free_minor(type);  
  23. #else  
  24.     minor = snd_kernel_minor(type, card, dev);  
  25.     if (minor >= 0 && snd_minors[minor])  
  26.         minor = -EBUSY;  
  27. #endif  
  28.     if (minor < 0) {  
  29.         mutex_unlock(&sound_mutex);  
  30.         kfree(preg);  
  31.         return minor;  
  32.     }  
  33.     snd_minors[minor] = preg;  
  34.     preg->dev = device_create(sound_class, device, MKDEV(major, minor),  
  35.                   private_data, "%s", name);  
  36.     if (IS_ERR(preg->dev)) {  
  37.         snd_minors[minor] = NULL;  
  38.         mutex_unlock(&sound_mutex);  
  39.         minor = PTR_ERR(preg->dev);  
  40.         kfree(preg);  
  41.         return minor;  
  42.     }  
  43.   
  44.     mutex_unlock(&sound_mutex);  
  45.     return 0;  
  46. }  
1.   首先上来就分配一个snd_minor结构体。
2.   根据传递进来的参数,各种参数。对于pcm设备来说,当前的private_data就是pcm。此处需要重点介绍file_operations结构。此函数最终会在应用程序调用open的时候走到此处
[cpp]  view plain  copy
  1. const struct file_operations snd_pcm_f_ops[2] = {  

概述

1.  什么是pcm?
pcm(Pulse-code modulation)脉冲编码调制,是将模拟信号转化为数字信号的一种方法。声音的转化的过程为,先对连续的模拟信号按照固定频率周期性采样,将采样到的数据按照一定的精度进行量化,量化后的信号和采样后的信号差值叫做量化误差,将量化后的数据进行最后的编码存储,最终模拟信号变化为数字信号。

2. pcm的两个重要属性
    a.  采样率:        单位时间内采样的次数,采样频率越高越高,
    b.  采样位数:    一个采样信号的位数,也是对采样精度的变现。

对于人类而言,能接受声音的频率范围是20Hz-20KHz, 所以采样的频率44.1KHz 以及16bit的采样位数就可以有很好的保真能力(CD格式的采样率和采样位数)。


                                                              图1-1  声音的录音和播放过程

数据结构

在ALSA架构下,pcm也被称为设备,所谓的逻辑设备。在linux系统中使用snd_pcm结构表示一个pcm设备。
[cpp]  view plain  copy
  1. struct snd_pcm {  
  2.     struct snd_card *card;  
  3.     struct list_head list;  
  4.     int device; /* device number */  
  5.     unsigned int info_flags;  
  6.     unsigned short dev_class;  
  7.     unsigned short dev_subclass;  
  8.     char id[64];  
  9.     char name[80];  
  10.     struct snd_pcm_str streams[2];  
  11.     struct mutex open_mutex;  
  12.     wait_queue_head_t open_wait;  
  13.     void *private_data;  
  14.     void (*private_free) (struct snd_pcm *pcm);  
  15.     struct device *dev; /* actual hw device this belongs to */  
  16.     bool internal; /* pcm is for internal use only */  
  17.     bool nonatomic; /* whole PCM operations are in non-atomic context */  
  18. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)  
  19.     struct snd_pcm_oss oss;  
  20. #endif  
  21. };  
.card:         此pcm设备所属的card。
.list:           用于将pcm设备链接起来,最终所有的pcm设备会放入snd_pcm_devices链表中。
.device:      该pcm的索引号。
.id:             该pcm的标识。
.streams:   指向pcm的capture和playback stream,通常0代表playback,1代表capture。

通常一个pcm下会有两个stream, 分别为capture stream和playback stream,在每个stream下又会存在多个substream。
linux系统中使用snd_pcm_str定义stream, 使用snd_pcm_substream定义substream。
[cpp]  view plain  copy
  1. struct snd_pcm_str {  
  2.     int stream;             /* stream (direction) */  
  3.     struct snd_pcm *pcm;  
  4.     /* -- substreams -- */  
  5.     unsigned int substream_count;  
  6.     unsigned int substream_opened;  
  7.     struct snd_pcm_substream *substream;  
  8. };  
.stream:  当前stream的方向,capture or playback。
.pcm:      所属的pcm。
.substream_count:  该stream下substream的个数。
.substream_opened:  该stream下open的substream个数。
.substream:  该stream下的substream.

[cpp]  view plain  copy
  1. struct snd_pcm_substream {  
  2.     struct snd_pcm *pcm;  
  3.     struct snd_pcm_str *pstr;  
  4.     void *private_data;     /* copied from pcm->private_data */  
  5.     int number;  
  6.     char name[32];          /* substream name */  
  7.     int stream;         /* stream (direction) */  
  8.     struct pm_qos_request latency_pm_qos_req; /* pm_qos request */  
  9.     size_t buffer_bytes_max;    /* limit ring buffer size */  
  10.     struct snd_dma_buffer dma_buffer;  
  11.     size_t dma_max;  
  12.     /* -- hardware operations -- */  
  13.     const struct snd_pcm_ops *ops;  
  14.     /* -- runtime information -- */  
  15.     struct snd_pcm_runtime *runtime;  
  16.         /* -- timer section -- */  
  17.     struct snd_timer *timer;        /* timer */  
  18.     unsigned timer_running: 1;  /* time is running */  
  19.     /* -- next substream -- */  
  20.     struct snd_pcm_substream *next;  
  21.     /* -- linked substreams -- */  
  22.     struct list_head link_list; /* linked list member */  
  23.     struct snd_pcm_group self_group;    /* fake group for non linked substream (with substream lock inside) */  
  24.     struct snd_pcm_group *group;        /* pointer to current group */  
  25.     /* -- assigned files -- */  
  26.     void *file;  
  27.     int ref_count;  
  28.     atomic_t mmap_count;  
  29.     unsigned int f_flags;  
  30.     void (*pcm_release)(struct snd_pcm_substream *);  
  31.     struct pid *pid;  
  32.     /* misc flags */  
  33.     unsigned int hw_opened: 1;  
  34. };  
.pcm:       所属的pcm。
.pstr:       所属的stream。
.id:           代表的该stream下第几个substream,也就是序号。
.stream:  该substream的方向流,是palyback or capture。
.name:     该substrem的名字。
.ops:        硬件操作函数集合。
.runtime:   运行时的pcm的一些信息。
.next:        用于链接下一个sub stream。

下图是对这几个结构体之间的简单表述。



pcm设备的创建

创建一个pcm设备的实例,使用snd_pcm_new函数。
[cpp]  view plain  copy
  1. /** 
  2.  * snd_pcm_new - create a new PCM instance 
  3.  * @card: the card instance 
  4.  * @id: the id string 
  5.  * @device: the device index (zero based) 
  6.  * @playback_count: the number of substreams for playback 
  7.  * @capture_count: the number of substreams for capture 
  8.  * @rpcm: the pointer to store the new pcm instance 
  9.  * 
  10.  * Creates a new PCM instance. 
  11.  * 
  12.  * The pcm operators have to be set afterwards to the new instance 
  13.  * via snd_pcm_set_ops(). 
  14.  * 
  15.  * Return: Zero if successful, or a negative error code on failure. 
  16.  */  
  17. int snd_pcm_new(struct snd_card *card, const char *id, int device,  
  18.         int playback_count, int capture_count, struct snd_pcm **rpcm)  
  19. {  
  20.     return _snd_pcm_new(card, id, device, playback_count, capture_count,  
  21.             false, rpcm);  
  22. }  
此函数会传入六个参数,其中该函数的注释写的很清楚,不做过多解释。函数最终会返回rpcm参数。
[cpp]  view plain  copy
  1. static int _snd_pcm_new(struct snd_card *card, const char *id, int device,  
  2.         int playback_count, int capture_count, bool internal,  
  3.         struct snd_pcm **rpcm)  
  4. {  
  5.     struct snd_pcm *pcm;  
  6.     int err;  
  7.     static struct snd_device_ops ops = {  
  8.         .dev_free = snd_pcm_dev_free,  
  9.         .dev_register = snd_pcm_dev_register,  
  10.         .dev_disconnect = snd_pcm_dev_disconnect,  
  11.     };  
  12.   
  13.     if (snd_BUG_ON(!card))  
  14.         return -ENXIO;  
  15.     if (rpcm)  
  16.         *rpcm = NULL;  
  17.     pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);  
  18.     if (pcm == NULL) {  
  19.         dev_err(card->dev, "Cannot allocate PCM\n");  
  20.         return -ENOMEM;  
  21.     }  
  22.     pcm->card = card;  
  23.     pcm->device = device;  
  24.     pcm->internal = internal;  
  25.     if (id)  
  26.         strlcpy(pcm->id, id, sizeof(pcm->id));  
  27.     if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {  
  28.         snd_pcm_free(pcm);  
  29.         return err;  
  30.     }  
  31.     if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {  
  32.         snd_pcm_free(pcm);  
  33.         return err;  
  34.     }  
  35.     mutex_init(&pcm->open_mutex);  
  36.     init_waitqueue_head(&pcm->open_wait);  
  37.     if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {  
  38.         snd_pcm_free(pcm);  
  39.         return err;  
  40.     }  
  41.     if (rpcm)  
  42.         *rpcm = pcm;  
  43.     return 0;  
  44. }  
1.  分配一个snd_pcm结构体。
2.  根据传递进来的参数设置card, device, internal, id。
3.  分别创建palyback & capture stream。
4.  调用snd_device_new接口创建pcm设备。

调用snd_pcm_new_stream创建一个stream
[cpp]  view plain  copy
  1. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count)  
  2. {  
  3.     int idx, err;  
  4.     struct snd_pcm_str *pstr = &pcm->streams[stream];  
  5.     struct snd_pcm_substream *substream, *prev;  
  6.   
  7. #if IS_ENABLED(CONFIG_SND_PCM_OSS)  
  8.     mutex_init(&pstr->oss.setup_mutex);  
  9. #endif  
  10.     pstr->stream = stream;  
  11.     pstr->pcm = pcm;  
  12.     pstr->substream_count = substream_count;  
  13.     if (substream_count > 0 && !pcm->internal) {  
  14.         err = snd_pcm_stream_proc_init(pstr);  
  15.         if (err < 0) {  
  16.             pcm_err(pcm, "Error in snd_pcm_stream_proc_init\n");  
  17.             return err;  
  18.         }  
  19.     }  
  20.     prev = NULL;  
  21.     for (idx = 0, prev = NULL; idx < substream_count; idx++) {  
  22.         substream = kzalloc(sizeof(*substream), GFP_KERNEL);  
  23.         if (substream == NULL) {  
  24.             pcm_err(pcm, "Cannot allocate PCM substream\n");  
  25.             return -ENOMEM;  
  26.         }  
  27.         substream->pcm = pcm;  
  28.         substream->pstr = pstr;  
  29.         substream->number = idx;  
  30.         substream->stream = stream;  
  31.         sprintf(substream->name, "subdevice #%i", idx);  
  32.         substream->buffer_bytes_max = UINT_MAX;  
  33.         if (prev == NULL)  
  34.             pstr->substream = substream;  
  35.         else  
  36.             prev->next = substream;  
  37.   
  38.         if (!pcm->internal) {  
  39.             err = snd_pcm_substream_proc_init(substream);  
  40.             if (err < 0) {  
  41.                 pcm_err(pcm,  
  42.                     "Error in snd_pcm_stream_proc_init\n");  
  43.                 if (prev == NULL)  
  44.                     pstr->substream = NULL;  
  45.                 else  
  46.                     prev->next = NULL;  
  47.                 kfree(substream);  
  48.                 return err;  
  49.             }  
  50.         }  
  51.         substream->group = &substream->self_group;  
  52.         spin_lock_init(&substream->self_group.lock);  
  53.         mutex_init(&substream->self_group.mutex);  
  54.         INIT_LIST_HEAD(&substream->self_group.substreams);  
  55.         list_add_tail(&substream->link_list, &substream->self_group.substreams);  
  56.         atomic_set(&substream->mmap_count, 0);  
  57.         prev = substream;  
  58.     }  
  59.     return 0;  
  60. }             
1.   根据传递进来的参数,设置pcm的stream, pcm, substream_count的值。
2.   在proc下创建pcm相关目录信息。会调用snd_pcm_stream_proc_init函数,根据stream的类型创建pcm0p/pcm0c文件夹,然后会在此文件夹下创建info文件。info文件的类型会通过snd_pcm_stream_proc_info_read函数获得。代表就不贴出来了。:(
[cpp]  view plain  copy
  1. root@test:/proc/asound/card0/pcm0c$ cat info   
  2. card: 0  
  3. device: 0  
  4. subdevice: 0  
  5. stream: CAPTURE  
  6. id: ALC662 rev1 Analog  
  7. name: ALC662 rev1 Analog  
  8. subname: subdevice #0  
  9. class: 0  
  10. subclass: 0  
  11. subdevices_count: 1  
  12. subdevices_avail: 1  
3.   会根据substrem_count的个数,进行for循环操作。
4.   分配一个substream结构,设置必要的参数,如:  pcm,  pstr,  number,  stream,  name等。
5.   调用snd_pcm_substream_proc_init函数,创建sub0目录,然后在此目录下创建info, hw_params, sw_params,status等文件。
6.   将所有的substream会通过linklist链表保存,同时如果有多个substream会通过next指针相连。

至此,pcm设备就全部创建完成,创建完成后会形成如下的逻辑试图。

大体上就是一棵树,根节点是card0, 然后子节点是pcm设备,pcm设备分为capture & playback stream, 然后在stream下又分为substrem。

PCM硬件操作函数集设置

实例化一个pcm设备之后,还需要通过snd_pcm_set_ops函数设置该硬件的操作集合。
[cpp]  view plain  copy
  1. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,  
  2.              const struct snd_pcm_ops *ops)  
  3. {  
  4.     struct snd_pcm_str *stream = &pcm->streams[direction];  
  5.     struct snd_pcm_substream *substream;  
  6.       
  7.     for (substream = stream->substream; substream != NULL; substream = substream->next)  
  8.         substream->ops = ops;  
  9. }  
该函数会根据当前stream的方向/类型,设置该硬件对应的snd_pcm_ops操作集合。

整个流程梳理


PCM设备节点创建

当调用snd_card_register的时候,就会依次调用card列表下每个设备的dev_register回调函数,对pcm设备来说就是在_snd_pcm_new函数中的
[cpp]  view plain  copy
  1. static struct snd_device_ops ops = {  
  2.     .dev_free = snd_pcm_dev_free,  
  3.     .dev_register = snd_pcm_dev_register,  
  4.     .dev_disconnect = snd_pcm_dev_disconnect,  
  5. };  
此时会调用到snd_pcm_dev_register回调处理函数。
[cpp]  view plain  copy
  1. static int snd_pcm_dev_register(struct snd_device *device)  
  2. {  
  3.     int cidx, err;  
  4.     struct snd_pcm_substream *substream;  
  5.     struct snd_pcm_notify *notify;  
  6.     char str[16];  
  7.     struct snd_pcm *pcm;  
  8.     struct device *dev;  
  9.   
  10.     if (snd_BUG_ON(!device || !device->device_data))  
  11.         return -ENXIO;  
  12.     pcm = device->device_data;  
  13.     mutex_lock(&register_mutex);  
  14.     err = snd_pcm_add(pcm);  
  15.     if (err) {  
  16.         mutex_unlock(&register_mutex);  
  17.         return err;  
  18.     }  
  19.     for (cidx = 0; cidx < 2; cidx++) {  
  20.         int devtype = -1;  
  21.         if (pcm->streams[cidx].substream == NULL || pcm->internal)  
  22.             continue;  
  23.         switch (cidx) {  
  24.         case SNDRV_PCM_STREAM_PLAYBACK:  
  25.             sprintf(str, "pcmC%iD%ip", pcm->card->number, pcm->device);  
  26.             devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;  
  27.             break;  
  28.         case SNDRV_PCM_STREAM_CAPTURE:  
  29.             sprintf(str, "pcmC%iD%ic", pcm->card->number, pcm->device);  
  30.             devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;  
  31.             break;  
  32.         }  
  33.         /* device pointer to use, pcm->dev takes precedence if 
  34.          * it is assigned, otherwise fall back to card's device 
  35.          * if possible */  
  36.         dev = pcm->dev;  
  37.         if (!dev)  
  38.             dev = snd_card_get_device_link(pcm->card);  
  39.         /* register pcm */  
  40.         err = snd_register_device_for_dev(devtype, pcm->card,  
  41.                           pcm->device,  
  42.                           &snd_pcm_f_ops[cidx],  
  43.                           pcm, str, dev);  
  44.         if (err < 0) {  
  45.             list_del(&pcm->list);  
  46.             mutex_unlock(&register_mutex);  
  47.             return err;  
  48.         }  
  49.   
  50.         dev = snd_get_device(devtype, pcm->card, pcm->device);  
  51.         if (dev) {  
  52.             err = sysfs_create_groups(&dev->kobj,  
  53.                           pcm_dev_attr_groups);  
  54.             if (err < 0)  
  55.                 dev_warn(dev,  
  56.                      "pcm %d:%d: cannot create sysfs groups\n",  
  57.                      pcm->card->number, pcm->device);  
  58.             put_device(dev);  
  59.         }  
  60.   
  61.         for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)  
  62.             snd_pcm_timer_init(substream);  
  63.     }  
  64.   
  65.     list_for_each_entry(notify, &snd_pcm_notify_list, list)  
  66.         notify->n_register(pcm);  
  67.   
  68.     mutex_unlock(&register_mutex);  
  69.     return 0;  
  70. }  
1.   合法性判断,对pcm设备来说,snd_device->device_data存放的是当前的pcm指针。
2.    会调用snd_pcm_add此函数,判断此pcm设备是存在snd_pcm_devices链表中存在,存在就返回错误,不存在就添加。
3.    设置当前pcm设备name, 以及具体的pcm设备类型,PCM_CAPTURE  or PCM_PLAYBACK。
4.    调用snd_register_device_for_dev添加pcm设备到系统中。
5.    调用snd_get_device此函数返回当前注册的pcm设备,然后设置该pcm的属性。
6.    调用snd_pcm_timer_init函数,进行pcm定时器的初始化。

在继续分析snd_register_device_for_dev函数之前需要先介绍一个结构体。struct snd_minor。
[cpp]  view plain  copy
  1. struct snd_minor {  
  2.     int type;           /* SNDRV_DEVICE_TYPE_XXX */  
  3.     int card;           /* card number */  
  4.     int device;         /* device number */  
  5.     const struct file_operations *f_ops;    /* file operations */  
  6.     void *private_data;     /* private data for f_ops->open */  
  7.     struct device *dev;     /* device for sysfs */  
  8.     struct snd_card *card_ptr;  /* assigned card instance */  
  9. };  
.type:  设备类型,比如是pcm, control, timer等设备。
.card_number:  所属的card。
.device:  当前设备类型下的设备编号。
.f_ops:  具体设备的文件操作集合。
.private_data:  open函数的私有数据。
.card_ptr:  所属的card。

此结构体是用来保存当前设备的上下文信息,该card下所有逻辑设备都存在此结构。

[cpp]  view plain  copy
  1. int snd_register_device_for_dev(int type, struct snd_card *card, int dev,  
  2.                 const struct file_operations *f_ops,  
  3.                 void *private_data,  
  4.                 const char *name, struct device *device)  
  5. {  
  6.     int minor;  
  7.     struct snd_minor *preg;  
  8.   
  9.     if (snd_BUG_ON(!name))  
  10.         return -EINVAL;  
  11.     preg = kmalloc(sizeof *preg, GFP_KERNEL);  
  12.     if (preg == NULL)  
  13.         return -ENOMEM;  
  14.     preg->type = type;  
  15.     preg->card = card ? card->number : -1;  
  16.     preg->device = dev;  
  17.     preg->f_ops = f_ops;  
  18.     preg->private_data = private_data;  
  19.     preg->card_ptr = card;  
  20.     mutex_lock(&sound_mutex);  
  21. #ifdef CONFIG_SND_DYNAMIC_MINORS  
  22.     minor = snd_find_free_minor(type);  
  23. #else  
  24.     minor = snd_kernel_minor(type, card, dev);  
  25.     if (minor >= 0 && snd_minors[minor])  
  26.         minor = -EBUSY;  
  27. #endif  
  28.     if (minor < 0) {  
  29.         mutex_unlock(&sound_mutex);  
  30.         kfree(preg);  
  31.         return minor;  
  32.     }  
  33.     snd_minors[minor] = preg;  
  34.     preg->dev = device_create(sound_class, device, MKDEV(major, minor),  
  35.                   private_data, "%s", name);  
  36.     if (IS_ERR(preg->dev)) {  
  37.         snd_minors[minor] = NULL;  
  38.         mutex_unlock(&sound_mutex);  
  39.         minor = PTR_ERR(preg->dev);  
  40.         kfree(preg);  
  41.         return minor;  
  42.     }  
  43.   
  44.     mutex_unlock(&sound_mutex);  
  45.     return 0;  
  46. }  
1.   首先上来就分配一个snd_minor结构体。
2.   根据传递进来的参数,各种参数。对于pcm设备来说,当前的private_data就是pcm。此处需要重点介绍file_operations结构。此函数最终会在应用程序调用open的时候走到此处
[cpp]  view plain  copy
  1. const struct file_operations snd_pcm_f_ops[2] = {  

猜你喜欢

转载自blog.csdn.net/daha1314/article/details/84997358