linux-alsa詳解2 pcm設備


1 pcm設備介紹

PCM是英文Pulse-code modulation的縮寫,中文譯名是脈沖編碼調制.我們知道在現實生活中,人耳聽到的聲音是模擬信號,PCM就是要把聲音從模擬轉換成數字信號的一種技術,他的原理簡單地說就是利用一個固定的頻率對模擬信號進行采樣,采樣后的信號在波形上看就像一串連續的幅值不一的脈沖,把這些脈沖的幅值按一定的精度進行量化,這些量化后的數值被連續地輸出、傳輸、處理或記錄到存儲介質中,所有這些組成了數字音頻的產生過程。

PCM信號的兩個重要指標是采樣頻率和量化精度,目前,CD音頻的采樣頻率通常為44100Hz,量化精度是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組成。

但是在嵌入式系統中,通常不會像上圖2中這么復雜,大多數情況下是一個聲卡,一個pcm實例,pcm下面有一個playback和capture stream,playback和capture下面各自有一個substream.

3 pcm的數據結構

定義位於:include\sound\pcm.h

3.1 struct snd_pcm

是掛在snd_card下面的一個snd_device。

 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];//該數組中的兩個元素指向兩個snd_pcm_str結構,分別代表playback stream和capture stream
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 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
18     struct snd_pcm_oss oss;
19 #endif
20 }

3.2 struct  snd_pcm_str

 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;//指向snd_pcm_substream結構
 8 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
 9     /* -- OSS things -- */
10     struct snd_pcm_oss_stream oss;
11 #endif
12 #ifdef CONFIG_SND_VERBOSE_PROCFS
13     struct snd_info_entry *proc_root;
14     struct snd_info_entry *proc_info_entry;
15 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
16     unsigned int xrun_debug;    /* 0 = disabled, 1 = verbose, 2 = stacktrace */
17     struct snd_info_entry *proc_xrun_debug_entry;
18 #endif
19 #endif
20     struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
21 }

3.3 struct snd_pcm_substream

是pcm中間層的核心,絕大部分任務都是在substream中處理,尤其是他的ops(snd_pcm_ops)字段,許多user空間的應用程序通過alsa-lib對驅動程序的請求都是由該結構中的函數處理.它的runtime字段則指向snd_pcm_runtime結構,snd_pcm_runtime記錄這substream的一些重要的軟件和硬件運行環境和參數。

 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     unsigned int dma_buf_id;
12     size_t dma_max;
13     /* -- hardware operations -- */
14     struct snd_pcm_ops *ops;//user空間的應用程序通過alsa-lib對驅動程序的請求都是由該結構中的函數處理
15     /* -- runtime information -- */
16     struct snd_pcm_runtime *runtime;//記錄這substream的一些重要的軟件和硬件運行環境和參數
17         /* -- timer section -- */
18     struct snd_timer *timer;        /* timer */
19     unsigned timer_running: 1;    /* time is running */
20     /* -- next substream -- */
21     struct snd_pcm_substream *next;
22     /* -- linked substreams -- */
23     struct list_head link_list;    /* linked list member */
24     struct snd_pcm_group self_group;    /* fake group for non linked substream (with substream lock inside) */
25     struct snd_pcm_group *group;        /* pointer to current group */
26     /* -- assigned files -- */
27     void *file;
28     int ref_count;
29     atomic_t mmap_count;
30     unsigned int f_flags;
31     void (*pcm_release)(struct snd_pcm_substream *);
32     struct pid *pid;
33 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
34     /* -- OSS things -- */
35     struct snd_pcm_oss_substream oss;
36 #endif
37 #ifdef CONFIG_SND_VERBOSE_PROCFS
38     struct snd_info_entry *proc_root;
39     struct snd_info_entry *proc_info_entry;
40     struct snd_info_entry *proc_hw_params_entry;
41     struct snd_info_entry *proc_sw_params_entry;
42     struct snd_info_entry *proc_status_entry;
43     struct snd_info_entry *proc_prealloc_entry;
44     struct snd_info_entry *proc_prealloc_max_entry;
45 #endif
46     /* misc flags */
47     unsigned int hw_opened: 1;
48 }

3.4 struct snd_pcm_ops

pcm設備的設備操作結構體

 1 struct snd_pcm_ops {
 2     int (*open)(struct snd_pcm_substream *substream);
 3     int (*close)(struct snd_pcm_substream *substream);
 4     int (*ioctl)(struct snd_pcm_substream * substream,
 5              unsigned int cmd, void *arg);
 6     int (*hw_params)(struct snd_pcm_substream *substream,
 7              struct snd_pcm_hw_params *params);
 8     int (*hw_free)(struct snd_pcm_substream *substream);
 9     int (*prepare)(struct snd_pcm_substream *substream);
10     int (*trigger)(struct snd_pcm_substream *substream, int cmd);
11     snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
12     int (*wall_clock)(struct snd_pcm_substream *substream,
13               struct timespec *audio_ts);
14     int (*copy)(struct snd_pcm_substream *substream, int channel,
15             snd_pcm_uframes_t pos,
16             void __user *buf, snd_pcm_uframes_t count);
17     int (*silence)(struct snd_pcm_substream *substream, int channel, 
18                snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
19     struct page *(*page)(struct snd_pcm_substream *substream,
20                  unsigned long offset);
21     int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma);
22     int (*ack)(struct snd_pcm_substream *substream);
23 }

3.5 struct snd_pcm_file

1 struct snd_pcm_file {
2     struct snd_pcm_substream *substream;
3     int no_compat_mmap;
4 };

3.6  struct snd_pcm_runtime

記錄這substream的一些重要的軟件和硬件運行環境和參數

 1 struct snd_pcm_runtime {
 2     /* -- Status -- */
 3     struct snd_pcm_substream *trigger_master;
 4     struct timespec trigger_tstamp;    /* trigger timestamp */
 5     int overrange;
 6     snd_pcm_uframes_t avail_max;
 7     snd_pcm_uframes_t hw_ptr_base;    /* Position at buffer restart */
 8     snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
 9     unsigned long hw_ptr_jiffies;    /* Time when hw_ptr is updated */
10     unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
11     snd_pcm_sframes_t delay;    /* extra delay; typically FIFO size */
12     u64 hw_ptr_wrap;                /* offset for hw_ptr due to boundary wrap-around */
13 
14     /* -- HW params -- */
15     snd_pcm_access_t access;    /* access mode */
16     snd_pcm_format_t format;    /* SNDRV_PCM_FORMAT_* */
17     snd_pcm_subformat_t subformat;    /* subformat */
18     unsigned int rate;        /* rate in Hz */
19     unsigned int channels;        /* channels */
20     snd_pcm_uframes_t period_size;    /* period size */
21     unsigned int periods;        /* periods */
22     snd_pcm_uframes_t buffer_size;    /* buffer size */
23     snd_pcm_uframes_t min_align;    /* Min alignment for the format */
24     size_t byte_align;
25     unsigned int frame_bits;
26     unsigned int sample_bits;
27     unsigned int info;
28     unsigned int rate_num;
29     unsigned int rate_den;
30     unsigned int no_period_wakeup: 1;
31 
32     /* -- SW params -- */
33     int tstamp_mode;        /* mmap timestamp is updated */
34       unsigned int period_step;
35     snd_pcm_uframes_t start_threshold;
36     snd_pcm_uframes_t stop_threshold;
37     snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
38                         noise is nearest than this */
39     snd_pcm_uframes_t silence_size;    /* Silence filling size */
40     snd_pcm_uframes_t boundary;    /* pointers wrap point */
41 
42     snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
43     snd_pcm_uframes_t silence_filled; /* size filled with silence */
44 
45     union snd_pcm_sync_id sync;    /* hardware synchronization ID */
46 
47     /* -- mmap -- */
48     struct snd_pcm_mmap_status *status;
49     struct snd_pcm_mmap_control *control;
50 
51     /* -- locking / scheduling -- */
52     snd_pcm_uframes_t twake;     /* do transfer (!poll) wakeup if non-zero */
53     wait_queue_head_t sleep;    /* poll sleep */
54     wait_queue_head_t tsleep;    /* transfer sleep */
55     struct fasync_struct *fasync;
56 
57     /* -- private section -- */
58     void *private_data;
59     void (*private_free)(struct snd_pcm_runtime *runtime);
60 
61     /* -- hardware description -- */
62     struct snd_pcm_hardware hw;
63     struct snd_pcm_hw_constraints hw_constraints;
64 
65     /* -- interrupt callbacks -- */
66     void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
67     void (*transfer_ack_end)(struct snd_pcm_substream *substream);
68 
69     /* -- timer -- */
70     unsigned int timer_resolution;    /* timer resolution */
71     int tstamp_type;        /* timestamp type */
72 
73     /* -- DMA -- */           
74     unsigned char *dma_area;    /* DMA area */
75     dma_addr_t dma_addr;        /* physical bus address (not accessible from main CPU) */
76     size_t dma_bytes;        /* size of DMA area */
77 
78     struct snd_dma_buffer *dma_buffer_p;    /* allocated buffer */
79 
80 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
81     /* -- OSS things -- */
82     struct snd_pcm_oss_runtime oss;
83 #endif
84 
85 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
86     struct snd_pcm_hwptr_log *hwptr_log;
87 #endif
88 }

3.7  struct snd_pcm_hardware

 1 /*
 2  *  Hardware (lowlevel) section
 3  */
 4 
 5 struct snd_pcm_hardware {
 6     unsigned int info;        /* SNDRV_PCM_INFO_* */
 7     u64 formats;            /* SNDRV_PCM_FMTBIT_* */
 8     unsigned int rates;        /* SNDRV_PCM_RATE_* */
 9     unsigned int rate_min;        /* min rate */
10     unsigned int rate_max;        /* max rate */
11     unsigned int channels_min;    /* min channels */
12     unsigned int channels_max;    /* max channels */
13     size_t buffer_bytes_max;    /* max buffer size */
14     size_t period_bytes_min;    /* min period size */
15     size_t period_bytes_max;    /* max period size */
16     unsigned int periods_min;    /* min # of periods */
17     unsigned int periods_max;    /* max # of periods */
18     size_t fifo_size;        /* fifo size in bytes */
19 }

3.8 以上pcm各數據結構的關系如下:

4 創建pcm設備

4.1 創建pcm

函數snd_pcm_new,定義位於:sound\core\pcm.c

 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) 參數device 表示目前創建的是該聲卡下的第幾個pcm,第一個pcm設備從0開始.
 6  * @playback_count: the number of substreams for playback 參數playback_count 表示該pcm將會有幾個playback substream.
 7  * @capture_count: the number of substreams for capture 參數capture_count 表示該pcm將會有幾個capture substream.
 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 }

調用函數 _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,這個回調函數會在聲卡的注冊階段被調用.

 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;//創建一個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         snd_printk(KERN_ERR "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) {//創建playback stream 播放
28         snd_pcm_free(pcm);
29         return err;
30     }
31     if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {//創建capture stream 錄音
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) {//把該pcm掛到聲卡中,參數ops中的dev_register字段指向了函數snd_pcm_dev_register,這個回調函數會在聲卡的注冊階段被調用
38         snd_pcm_free(pcm);
39         return err;
40     }
41     if (rpcm)
42         *rpcm = pcm;
43     return 0;
44 }

4.2 snd_device_new函數

定義位於:linux-4.9.73\sound\core\device.c

作用:(1)拿到聲卡邏輯設備的設備操作結構體

(2)將該設備邏輯設備加入聲卡結構體devices鏈表中(該聲卡下邏輯設備比如control、pcm等),dev->list加入card->devies,在注冊時根據此鏈表找到對應邏輯設備,調用其注冊函數。

 1 int snd_device_new(struct snd_card *card, enum snd_device_type type,
 2            void *device_data, struct snd_device_ops *ops)
 3 {
 4     struct snd_device *dev;
 5     struct list_head *p;
 6 
 7     if (snd_BUG_ON(!card || !device_data || !ops))
 8         return -ENXIO;
 9     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
10     if (!dev)
11         return -ENOMEM;
12     INIT_LIST_HEAD(&dev->list);
13     dev->card = card;
14     dev->type = type;
15     dev->state = SNDRV_DEV_BUILD;
16     dev->device_data = device_data;
17     dev->ops = ops;//拿到聲卡邏輯設備的設備操作結構體
18 
19     /* insert the entry in an incrementally sorted list */
20     list_for_each_prev(p, &card->devices) {//將該設備邏輯設備加入聲卡結構體devices鏈表中(該聲卡下邏輯設備比如control、pcm等),dev->list加入card->devies
21         struct snd_device *pdev = list_entry(p, struct snd_device, list);//獲取聲卡邏輯設備結構體
22         if ((unsigned int)pdev->type <= (unsigned int)type)//判斷該邏輯設備的類型,常用的control或者pcm
23             break;
24     }
25 
26     list_add(&dev->list, p);//在鏈表p的前面加入鏈表dev->list
27     return 0;
28 }

設備操作結構體定義,位於_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

定義位於:sound\core\pcm_lib.c

設置操作該pcm的控制/操作接口函數,參數中的snd_pcm_ops結構中的函數通常就是我們驅動要實現的函數。

 1 /**
 2  * snd_pcm_set_ops - set the PCM operators
 3  * @pcm: the pcm instance
 4  * @direction: stream direction, SNDRV_PCM_STREAM_XXX
 5  * @ops: the operator table
 6  *
 7  * Sets the given PCM operators to the pcm instance.
 8  */
 9 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
10 {
11     struct snd_pcm_str *stream = &pcm->streams[direction];
12     struct snd_pcm_substream *substream;
13     
14     for (substream = stream->substream; substream != NULL; substream = substream->next)
15         substream->ops = ops;
16 }

5 注冊pcm設備

在上面創建的pcm設備信息基礎上,注冊pcm設備。 

先調用函數snd_card_register注冊聲卡,然后該函數中會調用pcm注冊函數snd_pcm_dev_register注冊pcm設備。下面分別講解,先看pcm注冊函數。

5.1 pcm設備注冊函數

snd_pcm_dev_register,定義位於sound\core\pcm.c。

 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     struct snd_pcm *pcm;
 7 
 8     if (snd_BUG_ON(!device || !device->device_data))
 9         return -ENXIO;
10     pcm = device->device_data;
11     if (pcm->internal)
12         return 0;
13 
14     mutex_lock(&register_mutex);
15     err = snd_pcm_add(pcm);
16     if (err)
17         goto unlock;
18     for (cidx = 0; cidx < 2; cidx++) {
19         int devtype = -1;
20         if (pcm->streams[cidx].substream == NULL)
21             continue;
22         switch (cidx) {
23         case SNDRV_PCM_STREAM_PLAYBACK:
24             devtype = SNDRV_DEVICE_TYPE_PCM_PLAYBACK;
25             break;
26         case SNDRV_PCM_STREAM_CAPTURE:
27             devtype = SNDRV_DEVICE_TYPE_PCM_CAPTURE;
28             break;
29         }
30         /* register pcm */
31         err = snd_register_device(devtype, pcm->card, pcm->device,
32                       &snd_pcm_f_ops[cidx], pcm,//指定pcm設備的文件操作結構體
33                       &pcm->streams[cidx].dev);
34         if (err < 0) {
35             list_del_init(&pcm->list);
36             goto unlock;
37         }
38 
39         for (substream = pcm->streams[cidx].substream; substream; substream = substream->next)
40             snd_pcm_timer_init(substream);
41     }
42 
43     list_for_each_entry(notify, &snd_pcm_notify_list, list)
44         notify->n_register(pcm);
45 
46  unlock:
47     mutex_unlock(&register_mutex);
48     return err;
49 }

該pcm設備注冊函數snd_pcm_dev_register會加入設備操作結構體snd_device_ops 作為回調函數。

然后調用函數snd_register_device完成最終的注冊.

5.1.1 函數snd_register_device

 1 /**
 2  * snd_register_device - Register the ALSA device file for the card
 3  * @type: the device type, SNDRV_DEVICE_TYPE_XXX
 4  * @card: the card instance
 5  * @dev: the device index
 6  * @f_ops: the file operations
 7  * @private_data: user pointer for f_ops->open()
 8  * @device: the device to register
 9  *
10  * Registers an ALSA device file for the given card.
11  * The operators have to be set in reg parameter.
12  *
13  * Return: Zero if successful, or a negative error code on failure.
14  */
15 int snd_register_device(int type, struct snd_card *card, int dev,
16             const struct file_operations *f_ops, 17 void *private_data, struct device *device) 18 { 19 int minor; 20 int err = 0; 21 struct snd_minor *preg;//建立次設備 22 23 if (snd_BUG_ON(!device)) 24 return -EINVAL; 25 /*給次設備賦值*/ 26 preg = kmalloc(sizeof *preg, GFP_KERNEL); 27 if (preg == NULL) 28 return -ENOMEM; 29 preg->type = type; 30 preg->card = card ? card->number : -1; 31 preg->device = dev; 32 preg->f_ops = f_ops;//獲取文件操作結構體 33 preg->private_data = private_data; 34 preg->card_ptr = card; 35 mutex_lock(&sound_mutex); 36 minor = snd_find_free_minor(type, card, dev); 37 if (minor < 0) { 38 err = minor; 39 goto error; 40  } 41 42 preg->dev = device; 43 device->devt = MKDEV(major, minor); 44 err = device_add(device);//創建設備節點 45 if (err < 0) 46 goto error; 47 48 snd_minors[minor] = preg;//賦值給全局的次設備信息結構體 49  error: 50 mutex_unlock(&sound_mutex); 51 if (err < 0) 52  kfree(preg); 53 return err; 54 }

主要作用:

(1)獲取聲卡次設備snd_minor ,並將其賦值給全局聲卡次設備變量snd_minor中 

(2)獲取次設備pcm的文件操作結構體,供用戶層使用的api回調函數。snd_pcm_f_ops作為snd_register_device的參數被傳入,並被記錄在snd_minors[minor]中的字段f_ops中.最后創建設備節點。創建節點之后我們就能在/dev目錄下查看到相應的設備文件。

5.1.2 pcm設備的文件操作結構體

回調函數,供用戶層使用的api。分為playback和capature,即播放設備和錄音設備。

 1 /*
 2  *  Register section
 3  */
 4 
 5 const struct file_operations snd_pcm_f_ops[2] = {
 6     {
 7         .owner =        THIS_MODULE,
 8         .write =        snd_pcm_write,
 9         .write_iter =        snd_pcm_writev,
10         .open =            snd_pcm_playback_open,
11         .release =        snd_pcm_release,
12         .llseek =        no_llseek,
13         .poll =            snd_pcm_playback_poll,
14         .unlocked_ioctl =    snd_pcm_playback_ioctl,
15         .compat_ioctl =     snd_pcm_ioctl_compat,
16         .mmap =            snd_pcm_mmap,
17         .fasync =        snd_pcm_fasync,
18         .get_unmapped_area =    snd_pcm_get_unmapped_area,
19     },
20     {
21         .owner =        THIS_MODULE,
22         .read =            snd_pcm_read,
23         .read_iter =        snd_pcm_readv,
24         .open =            snd_pcm_capture_open,
25         .release =        snd_pcm_release,
26         .llseek =        no_llseek,
27         .poll =            snd_pcm_capture_poll,
28         .unlocked_ioctl =    snd_pcm_capture_ioctl,
29         .compat_ioctl =     snd_pcm_ioctl_compat,
30         .mmap =            snd_pcm_mmap,
31         .fasync =        snd_pcm_fasync,
32         .get_unmapped_area =    snd_pcm_get_unmapped_area,
33     }
34 }

然后看聲卡注冊函數。

5.2 聲卡注冊函數

snd_card_register 詳見:linux - alsa詳解1概括。dev/snd/pcmCxxDxxp、pcmCxxDxxc,設備文件節點的建立。注意和上面pcm設備注冊函數的區別,聲卡注冊函數最終會調用pcm注冊函數注冊pcm設備,或者調用control設備注冊函數注冊控制設備。

snd_card_register 函數調用函數snd_device_register_all

5.3 snd_device_register_all

 1 /*
 2  * register all the devices on the card.
 3  * called from init.c
 4  */
 5 int snd_device_register_all(struct snd_card *card)
 6 {
 7     struct snd_device *dev;
 8     int err;
 9     
10     if (snd_BUG_ON(!card))
11         return -ENXIO;
12     list_for_each_entry(dev, &card->devices, list) {//遍歷聲卡結構體中的設備鏈表,逐個注冊聲卡下掛載的邏輯設備
13         err = __snd_device_register(dev);
14         if (err < 0)
15             return err;
16     }
17     return 0;
18 }

5.4 函數__snd_device_register

 1 static int __snd_device_register(struct snd_device *dev)
 2 {
 3     if (dev->state == SNDRV_DEV_BUILD) {
 4         if (dev->ops->dev_register) {
 5             int err = dev->ops->dev_register(dev);//調用聲卡邏輯設備的注冊函數,即5.1中pcm設備注冊函數snd_pcm_dev_register
 6             if (err < 0)
 7                 return err;
 8         }
 9         dev->state = SNDRV_DEV_REGISTERED;
10     }
11     return 0;
12 }

注冊聲卡,在這個階段會遍歷聲卡下的所有邏輯設備,並且調用各設備的注冊回調函數,對於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

6 pcm設備的open

以從應用程序到驅動層pcm為例一步一步講解。

6.1 聲卡字符設備的注冊

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

 1 static int __init alsa_sound_init(void)
 2 {
 3     snd_major = major;
 4     snd_ecards_limit = cards_limit;
 5     if (register_chrdev(major, "alsa", &snd_fops)) {
 6         snd_printk(KERN_ERR "unable to register native major device number %d\n", major);
 7         return -EIO;
 8     }
 9     if (snd_info_init() < 0) {
10         unregister_chrdev(major, "alsa");
11         return -ENOMEM;
12     }
13     snd_info_minor_register();
14 #ifndef MODULE
15     printk(KERN_INFO "Advanced Linux Sound Architecture Driver Initialized.\n");
16 #endif
17     return 0;
18 }

register_chrdev中的參數major與之前創建pcm設備是device_create時的major是同一個,這樣的結果是,當應用程序open設備文件/dev/snd/pcmCxDxp時,會進入snd_fops的open回調函數。

6.2 打開pcm設備

從上一節中我們得知,open一個pcm設備時,將會調用snd_fops的open回調函數,我們先看看snd_fops的定義:

位於:

1 static const struct file_operations snd_fops =
2 {
3     .owner =    THIS_MODULE,
4     .open =        snd_open,
5     .llseek =    noop_llseek,
6 };

跟入snd_open函數,它首先從inode中取出此設備號,然后以次設備號為索引,從snd_minors全局數組中取出當初注冊pcm設備時填充的snd_minor結構(參看linux-alsa詳解1基本知識第4節的內容),然后從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結構中定義的回調。

 1 static int snd_open(struct inode *inode, struct file *file)
 2 {
 3     unsigned int minor = iminor(inode);
 4     struct snd_minor *mptr = NULL;
 5     const struct file_operations *old_fops;
 6     int err = 0;
 7 
 8     if (minor >= ARRAY_SIZE(snd_minors))
 9         return -ENODEV;
10     mutex_lock(&sound_mutex);
11     mptr = snd_minors[minor];
12     if (mptr == NULL) {
13         mptr = autoload_device(minor);
14         if (!mptr) {
15             mutex_unlock(&sound_mutex);
16             return -ENODEV;
17         }
18     }
19     old_fops = file->f_op;
20     file->f_op = fops_get(mptr->f_ops);
21     if (file->f_op == NULL) {
22         file->f_op = old_fops;
23         err = -ENODEV;
24     }
25     mutex_unlock(&sound_mutex);
26     if (err < 0)
27         return err;
28 
29     if (file->f_op->open) {
30         err = file->f_op->open(inode, file);
31         if (err) {
32             fops_put(file->f_op);
33             file->f_op = fops_get(old_fops);
34         }
35     }
36     fops_put(old_fops);
37     return err;
38 }

6.3 總結以上過程

下面的序列圖表示了應用程序如何最終調用到snd_pcm_f_ops結構中的回調函數:

參考博文:

https://www.cnblogs.com/jason-lu/archive/2013/06/07/3123750.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM