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 */
struct snd_card {
int number; /* number of soundcard (index to
snd_cards) */
char id[16]; /* id string of this card */
char driver[16]; /* driver name */
char shortname[32]; /* short name of this soundcard */
char longname[80]; /* name of this soundcard */
char mixername[80]; /* mixer name */
char components[128]; /* card components delimited with
space */
struct module *module; /* top-level module */
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 */
unsigned int last_numid; /* last used numeric ID */
struct rw_semaphore controls_rwsem; /* controls list lock */
rwlock_t ctl_files_rwlock; /* ctl_files list lock */
int controls_count; /* count of all controls */
int user_ctl_count; /* count of all user controls */
struct list_head controls; /* all controls for this card */
struct list_head ctl_files; /* active control files */
struct snd_info_entry *proc_root; /* root for soundcard specific files */
struct snd_info_entry *proc_id; /* the card id */
struct proc_dir_entry *proc_root_link; /* number link to real id */
struct list_head files_list; /* all files associated to this card */
struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown
state */
spinlock_t files_lock; /* lock the files for this card */
int shutdown; /* this card is going down */
int free_on_last_close; /* free in context of file_release */
wait_queue_head_t shutdown_sleep;
struct device *dev; /* device assigned to this card */
#ifndef CONFIG_SYSFS_DEPRECATED
struct device *card_dev; /* cardX object for sysfs */
#endif
#ifdef CONFIG_PM
unsigned int power_state; /* power state */
struct mutex power_lock; /* power lock */
wait_queue_head_t power_sleep;
#endif
#if defined(CONFIG_SND_MIXER_OSS) || defined(CONFIG_SND_MIXER_OSS_MODULE)
struct snd_mixer_oss *mixer_oss;
int mixer_oss_change_count;
#endif
};
struct list_head devices 记载该声卡下一切逻辑设备的链表
struct list_head controls 记载该声卡下一切的操控单元的链表
void *private_data 声卡的私有数据,能够在创立声卡时经过参数指定数据的巨细
2. 声卡的树立流程
2.1.1. 第一步,创立snd_card的一个实例
struct snd_card *card;
int err;
….
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 用于保存专用数据
err = snd_card_create(index, id, THIS_MODULE,
sizeof(struct mychip), &card);
// 从private_data中取出
struct mychip *chip = card->private_data;
自己创立:
struct mychip {
struct snd_card *card;
….
};
struct snd_card *card;
struct mychip *chip;
chip = kzalloc(sizeof(*chip), GFP_KERNEL);
……
err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
// 专用数据记载snd_card实例
chip->card = card;
…..
然后,把芯片的专有数据注册为声卡的一个低阶设备:
static int snd_mychip_dev_free(struct snd_device *device)
{
return snd_mychip_free(device->device_data);
}
static struct snd_device_ops ops = {
.dev_free = snd_mychip_dev_free,
};
….
snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
注册为低阶设备首要是为了当声卡被刊出时,芯片专用数据所占用的内存能够被自动地开释。
2.1.3. 第三步,设置Driver的ID和姓名
strcpy(card->driver, "My Chip");
strcpy(card->shortname, "My Own Chip 123");
sprintf(card->longname, "%s at 0x%lx irq %i",
card->shortname, chip->ioport, chip->irq);
snd_card的driver字段保存着芯片的ID字符串,user空间的alsa-lib会使用到该字符串,所以必需要确保该ID的唯一性。shortname字段更多地用于打印信息,longname字段则会出现在/proc/asound/cards中。