Linux ALSA音效卡驅動之三:PCM裝置的建立
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例項由一個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 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的一些重要的軟體和硬體執行環境和引數。
- 相關資料結構主要定義如下:
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定義如下:
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定義如下:
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中定義。
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指標的全域性陣列:
static struct snd_minor *snd_minors[256];
前面說過,在音效卡的註冊階段(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_register_device_for_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:指向該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()中:
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,它就是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),
private_data, "%s", name);
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_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的定義:
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結構中定義的回撥。
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的定義如下:
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)
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);
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>