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

    xiaoxiao2021-12-14  15

    1. PCM是什么


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

               PCM信号的两个重要指标是采样频率和量化精度,目前,CD音频的采样频率通常为44100Hz,量化精度是16bit。通常,播放音乐时,应用程序从存储介质中读取音频数据(MP3、WMA、AAC......),经过解码后,最终送到音频驱动程序中的就是PCM数据,反过来,在录音时,音频驱动不停地把采样所得的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组成。

                        图2.1  声卡中的pcm结构

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

           下面一张图列出了pcm中间层几个重要的结构,他可以让我们从uml的角度看一看这列结构的关系,理清他们之间的关系,对我们理解pcm中间层的实现方式。

     

                 图2.2  pcm中间层的几个重要的结构体的关系图 

    snd_pcm是挂在snd_card下面的一个snd_device,此snd_device保存在snd_card->devices列表中,snd_pcm保存在snd_device->device_data中。snd_pcm中的字段:streams[2],该数组中的两个元素指向两个snd_pcm_str结构,分别代表playback stream和capture streamsnd_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的一些重要的软件和硬件运行环境和参数。相关数据结构主要定义如下: [cpp] view plain copy struct snd_card {          ...      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: snd_device列表*/          ...  };    struct snd_device {      struct list_head list;      /* list of registered devices */      struct snd_card *card;      /* card which holds this device */      snd_device_state_t state;   /* state of the device */      snd_device_type_t type;     /* device type */      void *device_data;      /* device structure: 保存具体snd_device对象指针,如snd_pcm */      struct snd_device_ops *ops; /* operations:存有具体snd_device的操作,如snd_pcm*/  };    struct snd_device_ops {      int (*dev_free)(struct snd_device *dev);      int (*dev_register)(struct snd_device *dev);           /* dev_register: 在snd_card_register时被调用,且创建/dev/snd下的设备文件节点 */      int (*dev_disconnect)(struct snd_device *dev);  };    如snd_pcm的snd_device_ops为:      static struct snd_device_ops ops = {          .dev_free = snd_pcm_dev_free,          .dev_register = snd_pcm_dev_register,          .dev_disconnect = snd_pcm_dev_disconnect,      };    // pcm设备相关数据结构:  struct snd_pcm {      struct snd_card *card;          ...               struct snd_pcm_str streams[2];          ...  };    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;  /* substream 列表 */          ...  };    struct snd_pcm_substream {          ...      /* -- hardware operations -- */      struct snd_pcm_ops *ops;    /* 驱动对数据的操作 */        /* -- runtime information -- */      struct snd_pcm_runtime *runtime; /* 如通道数、采样率等信息 */              /* -- next substream -- */      struct snd_pcm_substream *next;  /* 通过它构成了substream链表 */      /* -- linked substreams -- */      struct list_head link_list; /* linked list member */  };    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,               unsigned int cmd, void *arg);      int (*hw_params)(struct snd_pcm_substream *substream,               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);      snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);      int (*copy)(struct snd_pcm_substream *substream, int channel,              snd_pcm_uframes_t pos,              void __user *buf, snd_pcm_uframes_t count);      int (*silence)(struct snd_pcm_substream *substream, int channel,                  snd_pcm_uframes_t pos, snd_pcm_uframes_t count);      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);  }  

     

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

    参数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);

             --设定指定方向的snd_pcm_str中的每个snd_pcm_substream的操作为此snd_pcm_ops,snd_pcm_ops定义如下: 

    [cpp] view plain copy 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,               unsigned int cmd, void *arg);      int (*hw_params)(struct snd_pcm_substream *substream,               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);      snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);      int (*copy)(struct snd_pcm_substream *substream, int channel,              snd_pcm_uframes_t pos,              void __user *buf, snd_pcm_uframes_t count);      int (*silence)(struct snd_pcm_substream *substream, int channel,                  snd_pcm_uframes_t pos, snd_pcm_uframes_t count);      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);  }  

            新建一个pcm可以用下面一张新建pcm的调用的序列图进行描述:

                                                           图3.1 新建pcm的序列图上

           上图中snd_device_new中的ops定义如下:

    [cpp] view plain copy 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_card_create    pcm是声卡下的一个设备(部件),所以第一步是要创建一个声卡snd_pcm_new    调用该api创建一个pcm,在该api中会做以下事情 如果有,建立playback stream,相应的substream也同时建立如果有,建立capture stream,相应的substream也同时建立调用snd_device_new()把该pcm挂到声卡的snd_card->devices链表中,参数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中定义。

    [cpp] view plain copy 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, 如snd_pcm对象 */      struct device *dev;     /* device for sysfs */  };  

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

    [cpp] view plain copy static struct snd_minor *snd_minors[256];  

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

    [cpp] view plain copy 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_register_device_for_dev():

    [cpp] view plain copy 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_CAPTUREcard: card的编号 device:pcm实例的编号,大多数情况为0 f_ops:snd_pcm_f_ops private_data:指向该snd_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()中:

    [cpp] view plain copy 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);                    ......      }           ......  }  

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

    playback  --  pcmCxDxp,通常系统中只有一个声卡和一个pcm,它就是pcmC0D0pcapture  --  pcmCxDxc,通常系统中只有一个声卡和一个pcm,它就是pcmC0D0c

    snd_pcm_f_ops

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

    [cpp] view plain copy 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中创建设备节点

    [cpp] view plain copy snd_minors[minor] = preg;  preg->dev = device_create(sound_class, device, MKDEV(major, minor),                private_data, "%s", name);  

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


    4.3.1 字符设备注册

          在sound/core/sound.c中有alsa_sound_init()函数,定义如下:

    [cpp] view plain copy 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_chrdev中的参数major与之前创建pcm设备是device_create时的major是同一个<即116>,这样的结果是,当应用程序open设备文件/dev/snd/pcmCxDxp时,会进入snd_fops的open回调函数,我们将在下一节中讲述open的过程。

    4.3.2 打开pcm设备

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

    [cpp] view plain copy static const struct file_operations snd_fops =  {      .owner =    THIS_MODULE,      .open =     snd_open  };  

         进入snd_open函数,它首先从inode中取出次设备号,然后以次设备号为索引,从snd_minors全局数组中取出当初注册pcm设备时填充的snd_minor结构(参看4.1节的内容),然后从snd_minor结构中取出pcm设备的f_ops,并且把file->f_op替换为pcm设备的f_ops,紧接着直接调用pcm设备的f_ops->open(),然后返回。因为file->f_op已经被替换,以后,应用程序的所有read/write/ioctl调用都会进入pcm设备自己的回调函数中,也就是4.2节中提到的snd_pcm_f_ops结构中定义的回调

    [cpp] view plain copy 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;  }  

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

                                 图4.3.2.1    应用程序操作pcm设备

        在上图中,file->f_op为snd_pcm_f_ops[0]或snd_pcm_f_ops[1],以playback为例,接下来的调用顺序为:

    1) snd_pcm_playback_open(struct inode *inode, struct file *file)->    /* snd_pcm根据文件节点的minor从snd_minors中获取*/

    2) snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)->

    3) static int snd_pcm_open_file(struct file *file, struct snd_pcm *pcm, int stream, struct snd_pcm_file **rpcm_file)-> snd_pcm_file的定义如下:

    [cpp] view plain copy struct snd_pcm_file {   struct snd_pcm_substream *substream;   int no_compat_mmap;  };  

      a) 调用snd_pcm_open_substream根据stream获取对应的snd_pcm_substream   b) 创建一个pcm_file对象,并把a)中获取的snd_pcm_substream赋值给pcm_file->substream   c) file->private_data = pcm_file, 这样根据file->private_data就可以找到对应的snd_pcm_substream   d) 然后就可以调用snd_pcm_substream->ops执行具体的操作

    4.3.3 写PCM设备

    写PCM流程如下:

    1) snd_pcm_write-> (用户态) 2) write-> (系统调用) 以下为Kernel态: 3) snd_pcm_write(struct file *file, const char __user *buf,         size_t count, loff_t * offset)    a) 从file中获取pcm_file    b) 从pcm_file中获取snd_pcm_substream 4) snd_pcm_lib_write(struct snd_pcm_substream *substream,                  const void __user *buf, snd_pcm_uframes_t size) 5) snd_pcm_lib_write1(struct snd_pcm_substream *substream,          unsigned long data,          snd_pcm_uframes_t size,          int nonblock,          transfer_f transfer)    即:snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,       snd_pcm_lib_write_transfer)   6) snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,           unsigned int hwoff,           unsigned long data, unsigned int off,           snd_pcm_uframes_t frames)

    [cpp] view plain copy static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,                        unsigned int hwoff,                        unsigned long data, unsigned int off,                        snd_pcm_uframes_t frames)  {      struct snd_pcm_runtime *runtime = substream->runtime;      int err;      char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);      if (substream->ops->copy) {          if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)              return err;      } else {          char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);          if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))              return -EFAULT;      }      return 0;  }  

         在此函数中调用snd_pcm_substream->ops->copy来传递数据给ALSA Driver,再由ALSA Driver把此数据发送给hardware playback。

    4.3.4 读PCM设备

    读PCM流程如下:

    1) snd_pcm_read-> (用户态) 2) read-> (系统调用) 以下为Kernel态: 3) snd_pcm_read(struct file *file, char __user *buf,                 size_t count,loff_t * offset) 4) snd_pcm_lib_read(struct snd_pcm_substream *substream,                    void __user *buf, snd_pcm_uframes_t size) 5) snd_pcm_lib_read1(struct snd_pcm_substream *substream,         unsigned long data,         snd_pcm_uframes_t size,         int nonblock,         transfer_f transfer)     即:snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock,                                   snd_pcm_lib_read_transfer);

    [cpp] view plain copy static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,                        unsigned int hwoff,                       unsigned long data, unsigned int off,                       snd_pcm_uframes_t frames)  {      struct snd_pcm_runtime *runtime = substream->runtime;      int err;      char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);      if (substream->ops->copy) {          if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)              return err;      } else {          char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);          if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))              return -EFAULT;      }      return 0;  }  

    5. 总结

        1) 通过device_create创建的设备文件节点中包含major和minor

        2) pcm设备的文件操作(file_operations snd_pcm_f_ops[2])被保存在snd_minors全局数据中,以minor为索引

        3) snd_minors的private_data为snd_pcm实例

        4) open文件节点时,根据其major寻找已经为此major注册的open函数,在此open函数中,则根据其minor在snd_minors中找到对应的f_ops,然后调用此f_ops->open<即snd_pcm_playback_open>

    转自:http://blog.csdn.net/droidphone/article/details/6308006

    转载请注明原文地址: https://ju.6miu.com/read-965289.html

    最新回复(0)