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

  1. /* main structure for soundcard */  
  2.   
  3. struct snd_card {  
  4.     int number;         /* number of soundcard (index to 
  5.                                 snd_cards) */  
  6.   
  7.     char id[16];            /* id string of this card */  
  8.     char driver[16];        /* driver name */  
  9.     char shortname[32];     /* short name of this soundcard */  
  10.     char longname[80];      /* name of this soundcard */  
  11.     char mixername[80];     /* mixer name */  
  12.     char components[128];       /* card components delimited with 
  13.                                 space */  
  14.     struct module *module;      /* top-level module */  
  15.   
  16.     void *private_data;     /* private data for soundcard */  
  17.     void (*private_free) (struct snd_card *card); /* callback for freeing of 
  18.                                 private data */  
  19.     struct list_head devices;   /* devices */  
  20.   
  21.     unsigned int last_numid;    /* last used numeric ID */  
  22.     struct rw_semaphore controls_rwsem; /* controls list lock */  
  23.     rwlock_t ctl_files_rwlock;  /* ctl_files list lock */  
  24.     int controls_count;     /* count of all controls */  
  25.     int user_ctl_count;     /* count of all user controls */  
  26.     struct list_head controls;  /* all controls for this card */  
  27.     struct list_head ctl_files; /* active control files */  
  28.   
  29.     struct snd_info_entry *proc_root;   /* root for soundcard specific files */  
  30.     struct snd_info_entry *proc_id; /* the card id */  
  31.     struct proc_dir_entry *proc_root_link;  /* number link to real id */  
  32.   
  33.     struct list_head files_list;    /* all files associated to this card */  
  34.     struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown 
  35.                                 state */  
  36.     spinlock_t files_lock;      /* lock the files for this card */  
  37.     int shutdown;           /* this card is going down */  
  38.     int free_on_last_close;     /* free in context of file_release */  
  39.     wait_queue_head_t shutdown_sleep;  
  40.     struct device *dev;     /* device assigned to this card */  
  41. #ifndef CONFIG_SYSFS_DEPRECATED  
  42.     struct device *card_dev;    /* cardX object for sysfs */  
  43. #endif  
  44.  
  45. #ifdef CONFIG_PM  
  46.     unsigned int power_state;   /* power state */  
  47.     struct mutex power_lock;    /* power lock */  
  48.     wait_queue_head_t power_sleep;  
  49. #endif  
  50.  
  51. #if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)  
  52.     struct snd_mixer_oss *mixer_oss;  
  53.     int mixer_oss_change_count;  
  54. #endif  
  55. };  
  • struct list_head devices     記錄該音效卡下所有邏輯裝置的鏈表
  • struct list_head controls    記錄該音效卡下所有的控制單元的鏈表
  • void *private_data            音效卡的私有資料,可以在建立音效卡時通過參數指定資料的大小

2. 音效卡的建立流程

 

2.1.1. 第一步,建立snd_card的一個實例

 

  1. struct snd_card *card;  
  2. int err;  
  3. ....  
  4. 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自己建立
  1. // struct mychip 用於保存專用資料  
  2. err = snd_card_create(index, id, THIS_MODULE,  
  3.                 sizeof(struct mychip), &card);  
  4. // 從private_data中取出  
  5. struct mychip *chip = card->private_data;  
  • 自己建立:
  1. struct mychip {  
  2.     struct snd_card *card;  
  3.     ....  
  4. };  
  5. struct snd_card *card;  
  6. struct mychip *chip;  
  7.   
  8. chip = kzalloc(sizeof(*chip), GFP_KERNEL);  
  9. ......  
  10. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);  
  11. // 專用資料記錄snd_card實例  
  12. chip->card = card;  
  13. .....  

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

  1. static int snd_mychip_dev_free(struct snd_device *device)  
  2. {  
  3.     return snd_mychip_free(device->device_data);  
  4. }  
  5.   
  6. static struct snd_device_ops ops = {  
  7.     .dev_free = snd_mychip_dev_free,  
  8. };  
  9. ....  
  10. snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);  

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

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

  1. strcpy(card->driver, "My Chip");  
  2. strcpy(card->shortname, "My Own Chip 123");  
  3. sprintf(card->longname, "%s at 0x%lx irq %i",  
  4.             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. 第五步,註冊音效卡

 

  1. err = snd_card_register(card);  
  2. if (err < 0) {  
  3.     snd_card_free(card);  
  4.     return err;  
  5. }  

 

 

2.2. 一個實際的例子

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

  1. static int __devinit pxa2xx_ac97_probe(struct platform_device *dev)  
  2. {  
  3.     struct snd_card *card;  
  4.     struct snd_ac97_bus *ac97_bus;  
  5.     struct snd_ac97_template ac97_template;  
  6.     int ret;  
  7.     pxa2xx_audio_ops_t *pdata = dev->dev.platform_data;  
  8.   
  9.     if (dev->id >= 0) {  
  10.         dev_err(&dev->dev, "PXA2xx has only one AC97 port./n");  
  11.         ret = -ENXIO;  
  12.         goto err_dev;  
  13.     }  
  14. ////(1)////  
  15.     ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,  
  16.                   THIS_MODULE, 0, &card);  
  17.     if (ret < 0)  
  18.         goto err;  
  19.   
  20.     card->dev = &dev->dev;  
  21. ////(3)////  
  22.     strncpy(card->driver, dev->dev.driver->name, sizeof(card->driver));  
  23.   
  24. ////(4)////  
  25.     ret = pxa2xx_pcm_new(card, &pxa2xx_ac97_pcm_client, &pxa2xx_ac97_pcm);  
  26.     if (ret)  
  27.         goto err;  
  28. ////(2)////  
  29.     ret = pxa2xx_ac97_hw_probe(dev);  
  30.     if (ret)  
  31.         goto err;  
  32.   
  33. ////(4)////  
  34.     ret = snd_ac97_bus(card, 0, &pxa2xx_ac97_ops, NULL, &ac97_bus);  
  35.     if (ret)  
  36.         goto err_remove;  
  37.     memset(&ac97_template, 0, sizeof(ac97_template));  
  38.     ret = snd_ac97_mixer(ac97_bus, &ac97_template, &pxa2xx_ac97_ac97);  
  39.     if (ret)  
  40.         goto err_remove;  
  41. ////(3)////  
  42.     snprintf(card->shortname, sizeof(card->shortname),  
  43.          "%s", snd_ac97_get_short_name(pxa2xx_ac97_ac97));  
  44.     snprintf(card->longname, sizeof(card->longname),  
  45.          "%s (%s)", dev->dev.driver->name, card->mixername);  
  46.   
  47.     if (pdata && pdata->codec_pdata[0])  
  48.         snd_ac97_dev_add_pdata(ac97_bus->codec[0], pdata->codec_pdata[0]);  
  49.     snd_card_set_dev(card, &dev->dev);  
  50. ////(5)////  
  51.     ret = snd_card_register(card);  
  52.     if (ret == 0) {  
  53.         platform_set_drvdata(dev, card);  
  54.         return 0;  
  55.     }  
  56.   
  57. err_remove:  
  58.     pxa2xx_ac97_hw_remove(dev);  
  59. err:  
  60.     if (card)  
  61.         snd_card_free(card);  
  62. err_dev:  
  63.     return ret;  
  64. }  
  65.   
  66. static int __devexit pxa2xx_ac97_remove(struct platform_device *dev)  
  67. {  
  68.     struct snd_card *card = platform_get_drvdata(dev);  
  69.   
  70.     if (card) {  
  71.         snd_card_free(card);  
  72.         platform_set_drvdata(dev, NULL);  
  73.         pxa2xx_ac97_hw_remove(dev);  
  74.     }  
  75.   
  76.     return 0;  
  77. }  
  78.   
  79. static struct platform_driver pxa2xx_ac97_driver = {  
  80.     .probe      = pxa2xx_ac97_probe,  
  81.     .remove     = __devexit_p(pxa2xx_ac97_remove),  
  82.     .driver     = {  
  83.         .name   = "pxa2xx-ac97",  
  84.         .owner  = THIS_MODULE,  
  85. #ifdef CONFIG_PM  
  86.         .pm = &pxa2xx_ac97_pm_ops,  
  87. #endif  
  88.     },  
  89. };  
  90.   
  91. static int __init pxa2xx_ac97_init(void)  
  92. {  
  93.     return platform_driver_register(&pxa2xx_ac97_driver);  
  94. }  
  95.   
  96. static void __exit pxa2xx_ac97_exit(void)  
  97. {  
  98.     platform_driver_unregister(&pxa2xx_ac97_driver);  
  99. }  
  100.   
  101. module_init(pxa2xx_ac97_init);  
  102. module_exit(pxa2xx_ac97_exit);  
  103.   
  104. MODULE_AUTHOR("Nicolas Pitre");  
  105. 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中定義。

  1. /** 
  2.  *  snd_card_create - create and initialize a soundcard structure 
  3.  *  @idx: card index (address) [0 ... (SNDRV_CARDS-1)] 
  4.  *  @xid: card identification (ASCII string) 
  5.  *  @module: top level module for locking 
  6.  *  @extra_size: allocate this extra size after the main soundcard structure 
  7.  *  @card_ret: the pointer to store the created card instance 
  8.  * 
  9.  *  Creates and initializes a soundcard structure. 
  10.  * 
  11.  *  The function allocates snd_card instance via kzalloc with the given 
  12.  *  space for the driver to use freely.  The allocated struct is stored 
  13.  *  in the given card_ret pointer. 
  14.  * 
  15.  *  Returns zero if successful or a negative error code. 
  16.  */  
  17. int snd_card_create(int idx, const char *xid,  
  18.             struct module *module, int extra_size,  
  19.             struct snd_card **card_ret)  

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

  1. card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);  
  2. if (!card)  
  3.     return -ENOMEM;  

拷貝音效卡的ID字串:

  1. if (xid)  
  2.     strlcpy(card->id, xid, sizeof(card->id));  

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

  1. if (idx < 0) {  
  2.     for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)  
  3.         /* idx == -1 == 0xffff means: take any free slot */  
  4.         if (~snd_cards_lock & idx & 1<<idx2) {  
  5.             if (module_slot_match(module, idx2)) {  
  6.                 idx = idx2;  
  7.                 break;  
  8.             }  
  9.         }  
  10. }  
  11. if (idx < 0) {  
  12.     for (idx2 = 0; idx2 < SNDRV_CARDS; idx2++)  
  13.         /* idx == -1 == 0xffff means: take any free slot */  
  14.         if (~snd_cards_lock & idx & 1<<idx2) {  
  15.             if (!slots[idx2] || !*slots[idx2]) {  
  16.                 idx = idx2;  
  17.                 break;  
  18.             }  
  19.         }  
  20. }  

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

  1.     card->number = idx;  
  2.     card->module = module;  
  3.     INIT_LIST_HEAD(&card->devices);  
  4.     init_rwsem(&card->controls_rwsem);  
  5.     rwlock_init(&card->ctl_files_rwlock);  
  6.     INIT_LIST_HEAD(&card->controls);  
  7.     INIT_LIST_HEAD(&card->ctl_files);  
  8.     spin_lock_init(&card->files_lock);  
  9.     INIT_LIST_HEAD(&card->files_list);  
  10.     init_waitqueue_head(&card->shutdown_sleep);  
  11. #ifdef CONFIG_PM  
  12.     mutex_init(&card->power_lock);  
  13.     init_waitqueue_head(&card->power_sleep);  
  14. #endif  

建立邏輯裝置:Control

  1. /* the control interface cannot be accessed from the user space until */  
  2. /* snd_cards_bitmask and snd_cards are set with snd_card_register */  
  3. err = snd_ctl_create(card);  

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

  1. err = snd_info_card_create(card);  

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

  1. if (extra_size > 0)  
  2.     card->private_data = (char *)card + sizeof(struct snd_card);  

4. snd_card_register()

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

  1. /** 
  2.  *  snd_card_register - register the soundcard 
  3.  *  @card: soundcard structure 
  4.  * 
  5.  *  This function registers all the devices assigned to the soundcard. 
  6.  *  Until calling this, the ALSA control interface is blocked from the 
  7.  *  external accesses.  Thus, you should call this function at the end 
  8.  *  of the initialization of the card. 
  9.  * 
  10.  *  Returns zero otherwise a negative error code if the registrain failed. 
  11.  */  
  12. int snd_card_register(struct snd_card *card)  

首先,建立sysfs下的設備:

  1. if (!card->card_dev) {  
  2.     card->card_dev = device_create(sound_class, card->dev,  
  3.                        MKDEV(0, 0), card,  
  4.                        "card%i", card->number);  
  5.     if (IS_ERR(card->card_dev))  
  6.         card->card_dev = NULL;  
  7. }  

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

  1. static char *sound_devnode(struct device *dev, mode_t *mode)  
  2. {  
  3.     if (MAJOR(dev->devt) == SOUND_MAJOR)  
  4.         return NULL;  
  5.     return kasprintf(GFP_KERNEL, "snd/%s", dev_name(dev));  
  6. }  
  7. static int __init init_soundcore(void)  
  8. {  
  9.     int rc;  
  10.   
  11.     rc = init_oss_soundcore();  
  12.     if (rc)  
  13.         return rc;  
  14.   
  15.     sound_class = class_create(THIS_MODULE, "sound");  
  16.     if (IS_ERR(sound_class)) {  
  17.         cleanup_oss_soundcore();  
  18.         return PTR_ERR(sound_class);  
  19.     }  
  20.   
  21.     sound_class->devnode = sound_devnode;  
  22.   
  23.     return 0;  
  24. }  

 

由此可見,音效卡的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()來實現各自設備的註冊的。

  1. if ((err = snd_device_register_all(card)) < 0)  
  2.     return err;  

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

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

 

  /*****************************************************************************************************/
聲明:本博內容均由http://blog.csdn.net/droidphone原創,轉載請注明出處,謝謝!
原文出處
http://blog.csdn.net/droidphone/article/details/6289712

/****

 

 

arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()