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
6
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的一个实例 [c-sharp] view plain copy 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实例的指针
7
2.1.2. 第二步,创建声卡的芯片专用数据
声卡的专用数据主要用于存放该声卡的一些资源信息,例如中断资源、io资源、dma资源等。可以有两种创建方法:
?
通过上一步中snd_card_create()中的第四个参数,让snd_card_create自己创建
[c-sharp] view plain copy 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;
?
自己创建:
[c-sharp] view plain copy
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. .....
然后,把芯片的专有数据注册为声卡的一个低阶设备: [c-sharp] view plain copy
1. static int snd_mychip_dev_free(struct snd_device *device)
8
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和名字 [c-sharp] view plain copy
1. strcpy(card->driver, \);
2. strcpy(card->shortname, \); 3. sprintf(card->longname, \,
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()
9
CONTROL -- snd_ctl_create() TIMER -- snd_timer_new() INFO -- snd_card_proc_new() JACK -- snd_jack_new() 2.1.5. 第五步,注册声卡
[c-sharp] view plain copy
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的部分代码贴上来: [cpp] view plain copy 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, \); 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)////
10