Linux ALSA音效卡驅動之二:音效卡的建立

來源:互聯網
上載者:User

聲明:本博內容均由http://blog.csdn.net/droidphone原創,轉載請註明出處,謝謝!

1. struct snd_card

 

1.1. snd_card是什麼

snd_card可以說是整個ALSA音頻驅動最頂層的一個結構,整個音效卡的軟體邏輯結構開始於該結構,幾乎所有與聲音相關的邏輯裝置都是在snd_card的管理之下,音效卡驅動的第一個動作通常就是建立一個snd_card結構體。正因為如此,本節中,我們也從 struct cnd_card開始吧。

 

1.2. snd_card的定義

snd_card的定義位於改標頭檔中:include/sound/core.h

/* main structure for soundcard */</p><p>struct snd_card {<br />int number;/* number of soundcard (index to<br />snd_cards) */</p><p>char id[16];/* id string of this card */<br />char driver[16];/* driver name */<br />char shortname[32];/* short name of this soundcard */<br />char longname[80];/* name of this soundcard */<br />char mixername[80];/* mixer name */<br />char components[128];/* card components delimited with<br />space */<br />struct module *module;/* top-level module */</p><p>void *private_data;/* private data for soundcard */<br />void (*private_free) (struct snd_card *card); /* callback for freeing of<br />private data */<br />struct list_head devices;/* devices */</p><p>unsigned int last_numid;/* last used numeric ID */<br />struct rw_semaphore controls_rwsem;/* controls list lock */<br />rwlock_t ctl_files_rwlock;/* ctl_files list lock */<br />int controls_count;/* count of all controls */<br />int user_ctl_count;/* count of all user controls */<br />struct list_head controls;/* all controls for this card */<br />struct list_head ctl_files;/* active control files */</p><p>struct snd_info_entry *proc_root;/* root for soundcard specific files */<br />struct snd_info_entry *proc_id;/* the card id */<br />struct proc_dir_entry *proc_root_link;/* number link to real id */</p><p>struct list_head files_list;/* all files associated to this card */<br />struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown<br />state */<br />spinlock_t files_lock;/* lock the files for this card */<br />int shutdown;/* this card is going down */<br />int free_on_last_close;/* free in context of file_release */<br />wait_queue_head_t shutdown_sleep;<br />struct device *dev;/* device assigned to this card */<br />#ifndef CONFIG_SYSFS_DEPRECATED<br />struct device *card_dev;/* cardX object for sysfs */<br />#endif</p><p>#ifdef CONFIG_PM<br />unsigned int power_state;/* power state */<br />struct mutex power_lock;/* power lock */<br />wait_queue_head_t power_sleep;<br />#endif</p><p>#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)<br />struct snd_mixer_oss *mixer_oss;<br />int mixer_oss_change_count;<br />#endif<br />};

  • struct list_head devices     記錄該音效卡下所有邏輯裝置的鏈表
  • struct list_head controls    記錄該音效卡下所有的控制單元的鏈表
  • void *private_data            音效卡的私人資料,可以在建立音效卡時通過參數指定資料的大小
2. 音效卡的建立流程

 

2.1.1. 第一步,建立snd_card的一個執行個體

 struct snd_card *card;<br />int err;<br />....<br />err = snd_card_create(index, id, THIS_MODULE, 0, &card);

  • index           一個整數值,該音效卡的編號
  • id                字串,音效卡的標識符
  • 第四個參數    該參數決定在建立snd_card執行個體時,需要同時額外分配的私人資料的大小,該資料的指標最終會賦值給snd_card的private_data資料成員
  • card             返回所建立的snd_card執行個體的指標

 

2.1.2. 第二步,建立音效卡的晶片專用資料

音效卡的專用資料主要用於存放該音效卡的一些資源資訊,例如中斷資源、io資源、dma資源等。可以有兩種建立方法:

  • 通過上一步中snd_card_create()中的第四個參數,讓snd_card_create自己建立

// struct mychip 用於儲存專用資料<br />err = snd_card_create(index, id, THIS_MODULE,<br /> sizeof(struct mychip), &card);<br />// 從private_data中取出<br />struct mychip *chip = card->private_data;

  • 自己建立:

struct mychip {<br /> struct snd_card *card;<br /> ....<br />};<br />struct snd_card *card;<br />struct mychip *chip;</p><p>chip = kzalloc(sizeof(*chip), GFP_KERNEL);<br />......<br />err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);<br />// 專用資料記錄snd_card執行個體<br />chip->card = card;<br />.....<br />

然後,把晶片的專有資料註冊為音效卡的一個低階裝置:

static int snd_mychip_dev_free(struct snd_device *device)<br />{<br /> return snd_mychip_free(device->device_data);<br />}</p><p>static struct snd_device_ops ops = {<br /> .dev_free = snd_mychip_dev_free,<br />};<br />....<br />snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);

 註冊為低階裝置主要是為了當音效卡被登出時,晶片專用資料所佔用的記憶體可以被自動地釋放。

2.1.3. 第三步,設定Driver的ID和名字

strcpy(card->driver, "My Chip");<br />strcpy(card->shortname, "My Own Chip 123");<br />sprintf(card->longname, "%s at 0x%lx irq %i",<br /> card->shortname, chip->ioport, chip->irq);

snd_card的driver欄位儲存著晶片的ID字串,user空間的alsa-lib會使用到該字串,所以必須要保證該ID的唯一性。shortname欄位更多地用於列印資訊,longname欄位則會出現在/proc/asound/cards中。

 

2.1.4. 第四步,建立音效卡的功能組件(邏輯裝置),例如PCM,Mixer,MIDI等

這時候可以建立音效卡的各種功能組件了,還記得開頭的snd_card結構體的devices欄位嗎?每一種組件的建立最終會調用snd_device_new()來產生一個snd_device執行個體,並把該執行個體連結到snd_card的devices鏈表中。

通常,alsa-driver的已經提供了一些常用的組件的建立函數,而不必直接調用snd_device_new(),比如:

    PCM  ----        snd_pcm_new()

    RAWMIDI --    snd_rawmidi_new()

    CONTROL --   snd_ctl_create()

    TIMER   --       snd_timer_new()

    INFO    --        snd_card_proc_new()

    JACK    --        snd_jack_new()

 

2.1.5. 第五步,註冊音效卡

 err = snd_card_register(card);<br />if (err < 0) {<br /> snd_card_free(card);<br /> return err;<br />}

 

 

2.2. 一個實際的例子

我把/sound/arm/pxa2xx-ac97.c的部分代碼貼上來:

static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)<br />{<br />struct snd_card *card;<br />struct snd_ac97_bus *ac97_bus;<br />struct snd_ac97_template ac97_template;<br />int ret;<br />pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;</p><p>if (dev->id >= 0) {<br />dev_err(&dev->dev, "PXA2xx has only one AC97 port./n");<br />ret = -ENXIO;<br />goto err_dev;<br />}<br />////(1)////<br />ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,<br /> THIS_MODULE, 0, &card);<br />if (ret < 0)<br />goto err;</p><p>card->dev = &dev->dev;<br />////(3)////<br />strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));</p><p>////(4)////<br />ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);<br />if (ret)<br />goto err;<br />////(2)////<br />ret = pxa2xx_ac97_hw_probe(dev);<br />if (ret)<br />goto err;</p><p>////(4)////<br />ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);<br />if (ret)<br />goto err_remove;<br />memset(&ac97_template, 0, sizeof(ac97_template));<br />ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);<br />if (ret)<br />goto err_remove;<br />////(3)////<br />snprintf(card->shortname, sizeof(card->shortname),<br /> "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));<br />snprintf(card->longname, sizeof(card->longname),<br /> "%s (%s)", dev->dev.driver->name, card->mixername);</p><p>if (pdata && pdata->codec_pdata[0])<br />snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);<br />snd_card_set_dev(card, &dev->dev);<br />////(5)////<br />ret = snd_card_register(card);<br />if (ret == 0) {<br />platform_set_drvdata(dev, card);<br />return 0;<br />}</p><p>err_remove:<br />pxa2xx_ac97_hw_remove(dev);<br />err:<br />if (card)<br />snd_card_free(card);<br />err_dev:<br />return ret;<br />}</p><p>static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)<br />{<br />struct snd_card *card = platform_get_drvdata(dev);</p><p>if (card) {<br />snd_card_free(card);<br />platform_set_drvdata(dev, NULL);<br />pxa2xx_ac97_hw_remove(dev);<br />}</p><p>return 0;<br />}</p><p>static struct platform_driver pxa2xx_ac97_driver = {<br />.probe= pxa2xx_ac97_probe,<br />.remove= __devexit_p(pxa2xx_ac97_remove),<br />.driver= {<br />.name= "pxa2xx-ac97",<br />.owner= THIS_MODULE,<br />#ifdef CONFIG_PM<br />.pm= &pxa2xx_ac97_pm_ops,<br />#endif<br />},<br />};</p><p>static int __init pxa2xx_ac97_init(void)<br />{<br />return platform_driver_register(&pxa2xx_ac97_driver);<br />}</p><p>static void __exit pxa2xx_ac97_exit(void)<br />{<br />platform_driver_unregister(&pxa2xx_ac97_driver);<br />}</p><p>module_init(pxa2xx_ac97_init);<br />module_exit(pxa2xx_ac97_exit);</p><p>MODULE_AUTHOR("Nicolas Pitre");<br />MODULE_DESCRIPTION("AC97 driver for the Intel PXA2xx chip");

 

 

驅動程式通常由probe回呼函數開始,對一下2.1中的步驟,是否有相似之處?

 

經過以上的建立步驟之後,音效卡的邏輯結構如所示:

                                             圖 2.2.1  音效卡的軟體邏輯結構

 

下面的章節裡我們分別討論一下snd_card_create()和snd_card_register()這兩個函數。

 

3. snd_card_create()

 snd_card_create()在/sound/core/init.c中定義。

/**<br /> * snd_card_create - create and initialize a soundcard structure<br /> * @idx: card index (address) [0 ... (SNDRV_CARDS-1)]<br /> * @xid: card identification (ASCII string)<br /> * @module: top level module for locking<br /> * @extra_size: allocate this extra size after the main soundcard structure<br /> * @card_ret: the pointer to store the created card instance<br /> *<br /> * Creates and initializes a soundcard structure.<br /> *<br /> * The function allocates snd_card instance via kzalloc with the given<br /> * space for the driver to use freely. The allocated struct is stored<br /> * in the given card_ret pointer.<br /> *<br /> * Returns zero if successful or a negative error code.<br /> */<br />int snd_card_create(int idx, const char *xid,<br /> struct module *module, int extra_size,<br /> struct snd_card **card_ret)

首先,根據extra_size參數的大小分配記憶體,該記憶體區可以作為晶片的專有資料使用(見前面的介紹):

card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);<br />if (!card)<br />return -ENOMEM;

拷貝音效卡的ID字串:

if (xid)<br />strlcpy(card->id, xid, sizeof(card->id));<br />

如果傳入的音效卡編號為-1,自動分配一個索引編號:

if (idx < 0) {<br />for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)<br />/* idx == -1 == 0xffff means: take any free slot */<br />if (~snd_cards_lock & idx & 1<<idx2) {<br />if (module_slot_match(module, idx2)) {<br />idx = idx2;<br />break;<br />}<br />}<br />}<br />if (idx < 0) {<br />for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)<br />/* idx == -1 == 0xffff means: take any free slot */<br />if (~snd_cards_lock & idx & 1<<idx2) {<br />if (!slots[idx2] || !*slots[idx2]) {<br />idx = idx2;<br />break;<br />}<br />}<br />}<br />

初始化snd_card結構中必要的欄位:

card->number = idx;<br />card->module = module;<br />INIT_LIST_HEAD(&card->devices);<br />init_rwsem(&card->controls_rwsem);<br />rwlock_init(&card->ctl_files_rwlock);<br />INIT_LIST_HEAD(&card->controls);<br />INIT_LIST_HEAD(&card->ctl_files);<br />spin_lock_init(&card->files_lock);<br />INIT_LIST_HEAD(&card->files_list);<br />init_waitqueue_head(&card->shutdown_sleep);<br />#ifdef CONFIG_PM<br />mutex_init(&card->power_lock);<br />init_waitqueue_head(&card->power_sleep);<br />#endif<br />

建立邏輯裝置:Control

/* the control interface cannot be accessed from the user space until */<br />/* snd_cards_bitmask and snd_cards are set with snd_card_register */<br />err = snd_ctl_create(card);<br />

建立proc檔案中的info節點:通常就是/proc/asound/card0

err = snd_info_card_create(card);<br />

把第一步分配的記憶體指標放入private_data欄位中:

if (extra_size > 0)<br />card->private_data = (char *)card + sizeof(struct snd_card);<br />

4. snd_card_register()

  snd_card_create()在/sound/core/init.c中定義。

/**<br /> * snd_card_register - register the soundcard<br /> * @card: soundcard structure<br /> *<br /> * This function registers all the devices assigned to the soundcard.<br /> * Until calling this, the ALSA control interface is blocked from the<br /> * external accesses. Thus, you should call this function at the end<br /> * of the initialization of the card.<br /> *<br /> * Returns zero otherwise a negative error code if the registrain failed.<br /> */<br />int snd_card_register(struct snd_card *card)<br />

首先,建立sysfs下的裝置:

if (!card->card_dev) {<br />card->card_dev = device_create(sound_class, card->dev,<br /> MKDEV(0, 0), card,<br /> "card%i", card->number);<br />if (IS_ERR(card->card_dev))<br />card->card_dev = NULL;<br />}<br />

其中,sound_class是在/sound/sound_core.c中建立的:

static char *sound_devnode(struct device *dev, mode_t *mode)<br />{<br />if (MAJOR(dev->devt) == SOUND_MAJOR)<br />return NULL;<br />return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));<br />}<br />static int __init init_soundcore(void)<br />{<br />int rc;</p><p>rc = init_oss_soundcore();<br />if (rc)<br />return rc;</p><p>sound_class = class_create(THIS_MODULE, "sound");<br />if (IS_ERR(sound_class)) {<br />cleanup_oss_soundcore();<br />return PTR_ERR(sound_class);<br />}</p><p>sound_class->devnode = sound_devnode;</p><p>return 0;<br />}<br />

 

由此可見,音效卡的class將會出現在檔案系統的/sys/class/sound/下面,並且,sound_devnode()也決定了相應的裝置節點也將會出現在/dev/snd/下面。

接下來的步驟,通過snd_device_register_all()註冊所有掛在該音效卡下的邏輯裝置,snd_device_register_all()實際上是通過snd_card的devices鏈表,遍曆所有的snd_device,並且調用snd_device的ops->dev_register()來實現各自裝置的註冊的。

if ((err = snd_device_register_all(card)) < 0)<br />return err;<br />

最後就是建立一些相應的proc和sysfs下的檔案或屬性節點,代碼就不貼了。

至此,整個音效卡完成了建立過程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.