轉自: http://blog.csdn.net/sepnic/article/details/6150723
control控制接口
控制接口對於許多開關(switch)和調節器(slider)應用廣泛,它能被用戶空間存取,從而讀寫CODEC相關寄存器。control的主要用於mixer。它用snd_kcontrol_new結構體描述。
snd_kcontrol_new
- struct snd_kcontrol_new {
- snd_ctl_elem_iface_t iface; /* interface identifier */
- unsigned int device; /* device/client number */
- unsigned int subdevice; /* subdevice (substream) number */
- unsigned char *name; /* ASCII name of item */
- unsigned int index; /* index of item */
- unsigned int access; /* access rights */
- unsigned int count; /* count of same elements */
- snd_kcontrol_info_t *info;
- snd_kcontrol_get_t *get;
- snd_kcontrol_put_t *put;
- union {
- snd_kcontrol_tlv_rw_t *c;
- const unsigned int *p;
- } tlv;
- unsigned long private_value;
- };
name字段是名稱標識,這個字段非常重要,因爲control的作用由名稱來區分,對於名稱相同的control,則使用index區分。下面會詳細介紹上層應用如何根據name名稱標識來找到底層相應的control。
name定義的標準是“SOURCE DIRECTION FUNCTION”即“源 方向 功能”,SOURCE定義了control的源,如“Master”、“PCM”等;DIRECTION 則爲“Playback”、“Capture”等,如果DIRECTION忽略,意味着Playback和capture雙向;FUNCTION則可以是“Switch”、“Volume”和“Route”等。
上層也可以根據numid來找到對應的control,snd_ctl_find_id()也是優先判斷上層是否傳遞了numid,是則直接返回這個numid對應的control。用戶層設置numid和control的關聯時,可用alsa-lib的snd_mixer_selem_set_enum_item()函數。snd_kcontrol_new結構體並沒有numid這個成員,是因爲numid是系統自動管理的,原則是該control的註冊次序,保存到snd_ctl_elem_value結構體中。
access字段是訪問控制權限。SNDRV_CTL_ELEM_ACCESS_READ意味着只讀,這時put()函數不必實現;SNDRV_CTL_ELEM_ACCESS_WRITE意味着只寫,這時get()函數不必實現。若control值頻繁變化,則需定義 VOLATILE標誌。當control處於非激活狀態時,應設置INACTIVE標誌。
private_value字段包含1個長整型值,可以通過它給info()、get()和put()函數傳遞參數。
kcontrol宏
在早期的ALSA創建一個新的control需要實現snd_kcontrol_new中的info、get和put這三個成員函數。現在較新版本的ALSA均定義了一些宏,如:
- #define SOC_SINGLE(xname, reg, shift, max, invert) /
- { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, /
- .info = snd_soc_info_volsw, .get = snd_soc_get_volsw,/
- .put = snd_soc_put_volsw, /
- .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
又如:
- #define SOC_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax, xinvert, tlv_array) /
- { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),/
- .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |/
- SNDRV_CTL_ELEM_ACCESS_READWRITE,/
- .tlv.p = (tlv_array), /
- .info = snd_soc_info_volsw_2r, /
- .get = snd_soc_get_volsw_2r, .put = snd_soc_put_volsw_2r, /
- .private_value = (unsigned long)&(struct soc_mixer_control) /
- {.reg = reg_left, .rreg = reg_right, .shift = xshift, /
- .max = xmax, .invert = xinvert} }
例如我們一個Playback Volume的kcontrol接口這樣定義:
SOC_DOUBLE_R_TLV("Playback Volume", REG_VOL_L, REG_VOL_R, 0, 192, 0, digital_tlv)
我們僅僅需要將Volume寄存器地址及位偏移,最大值填進去即可,當然這些數據要從Codec的datasheet取得。這裏Volume寄存器地址是REG_VOL_L(左聲道)和REG_VOL_R(右聲道),位偏移爲0,DAC Digital Gain範圍是0-192(steps)。
觸發過程
爲了探討這些kcontrol是如何觸發的,我們以SOC_DOUBLE_R_TLV的put函數爲例說明:
- /**
- * snd_soc_put_volsw_2r - double mixer set callback
- * @kcontrol: mixer control
- * @ucontrol: control element information
- *
- * Callback to set the value of a double mixer control that spans 2 registers.
- *
- * Returns 0 for success.
- */
- int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- struct soc_mixer_control *mc =
- (struct soc_mixer_control *)kcontrol->private_value;
- struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
- unsigned int reg = mc->reg;
- unsigned int reg2 = mc->rreg;
- unsigned int shift = mc->shift;
- int max = mc->max;
- unsigned int mask = (1 << fls(max)) - 1;
- unsigned int invert = mc->invert;
- int err;
- unsigned int val, val2, val_mask;
- val_mask = mask << shift;
- val = (ucontrol->value.integer.value[0] & mask);
- val2 = (ucontrol->value.integer.value[1] & mask);
- if (invert) {
- val = max - val;
- val2 = max - val2;
- }
- val = val << shift;
- val2 = val2 << shift;
- err = snd_soc_update_bits(codec, reg, val_mask, val);
- if (err < 0)
- return err;
- err = snd_soc_update_bits(codec, reg2, val_mask, val2);
- return err;
- }
shift是位偏移,而位掩碼mask是通過宏SOC_DOUBLE_R_TLV中的xmax運算得到:unsigned int mask = (1 << fls(max)) - 1;
調用snd_soc_update_bits()->snd_soc_write()將ucontrol的value送到CODEC的寄存器上。
snd_soc_put_volsw_2r()作爲一個callback函數,用戶層要設置某些功能時,如改變Playback Volume:
#amixer cset numid=3,iface=MIXER,name='Playback Volume' 100
注:amixer相關用法見:http://hi.baidu.com/serial_story/blog/item/c4e826d82a562f3f32fa1c31.html
補充,如果想查看當前的路由信息,可以打印;kcontrol->id.name這個就是你註冊的snd_control的具體名字
到內核層時,會遍歷一個節點類型爲struct snd_kcontrol *的鏈表,找到kcontrol.id.numid與3相匹配的kctl(這個過程見snd_ctl_find_id()函數),然後調用kctl.put()函數將100寫到Playback Volume寄存器中。當然如果上層沒有提供numid,則可根據name找到kcontrol.id.name相匹配的kctl。詳細見snd_ctl_find_id函數:
- /**
- * snd_ctl_find_id - find the control instance with the given id
- * @card: the card instance
- * @id: the id to search
- *
- * Finds the control instance with the given id from the card.
- *
- * Returns the pointer of the instance if found, or NULL if not.
- *
- * The caller must down card->controls_rwsem before calling this function
- * (if the race condition can happen).
- */
- struct snd_kcontrol *snd_ctl_find_id(struct snd_card *card,
- struct snd_ctl_elem_id *id)
- {
- struct snd_kcontrol *kctl;
- if (snd_BUG_ON(!card || !id))
- return NULL;
- if (id->numid != 0)
- return snd_ctl_find_numid(card, id->numid);
- list_for_each_entry(kctl, &card->controls, list) {
- if (kctl->id.iface != id->iface)
- continue;
- if (kctl->id.device != id->device)
- continue;
- if (kctl->id.subdevice != id->subdevice)
- continue;
- if (strncmp(kctl->id.name, id->name, sizeof(kctl->id.name)))
- continue;
- if (kctl->id.index > id->index)
- continue;
- if (kctl->id.index + kctl->count <= id->index)
- continue;
- return kctl;
- }
- return NULL;
- }
從上往下的大致流程:
- amixer-用戶層
- |->snd_ctl_ioctl-系統調用
- |->snd_ctl_elem_write_user-內核鉤子函數
- |->snd_ctl_elem_wirte-
- |->snd_ctl_find_id-遍歷kcontrol鏈表找到name字段匹配的kctl
- |->kctl->put()-調用kctl的成員函數put()
- |->snd_soc_put_volsw_2r