關於CIS需要清楚的:
1、CIS是什麼
CIS-Card Information Structure的縮寫,CIS的作用:
The CIS includes information on power, function, manufacturer and other things the host
needs to determine if the I/O function(s) is appropriate to power-up.
2、CIS的呈現
CIS位於CIA區域,地址尋址範圍0x001000~0x017FFF。CIS有兩類:
(1)Common CIS,記錄整張card的通用信息,比如廠商、VID等信息。
(2)Function CIS,每一個Function的特有信息。
一個CIS元素的組織結構:
它由結構體sdio_func_tuple表徵:
/*
* SDIO function CIS tuple (unknown to the core)
*/
struct sdio_func_tuple {
struct sdio_func_tuple *next;
unsigned char code;
unsigned char size;
unsigned char data[0];
};
這裏總結下Function:Function代表card的一個具體功能,每一個card具有多個Function,Function的標號爲0~7,特別地,0號針對的是CIA(可以簡單理解就是用來訪問CIA區域的)。在軟件層面,Function由struct sdio_func表示,它作爲一個device註冊到系統中。
3、CIS的訪問
需要兩個條件:命令CMD52;CIS地址。
對於Common CIS,其地址保存在CCCR的0x09~0x0B地址處的寄存器中,3個寄存器值組成一個24位的地址。
對於Function CIS,其地址保存在每一個Function的FBR(Function Basic Registers)地址0x109-0x10B中。
軟件層面,通過sdio_read_cis函數讀取,函數原型:
static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
如果參數func爲NULL,訪問的是Common CIS;否則則爲指定Function的CIS。4、CIS的解析
這正是接下來的重點要說的。
static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
{
int ret;
struct sdio_func_tuple *this, **prev;
unsigned i, ptr = 0;
/*
* Note that this works for the common CIS (function number 0) as
* well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
* have the same offset.
*/
for (i = 0; i < 3; i++) {
unsigned char x, fn;
if (func)
fn = func->num;
else
fn = 0;
ret = mmc_io_rw_direct(card, 0, 0,
SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
if (ret)
return ret;
ptr |= x << (i * 8);
}
if (func)
prev = &func->tuples;
else
prev = &card->tuples;
BUG_ON(*prev);
do {
unsigned char tpl_code, tpl_link;
ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
if (ret)
break;
/* 0xff means we're done */
if (tpl_code == 0xff)
break;
/* null entries have no link field or data */
if (tpl_code == 0x00)
continue;
ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
if (ret)
break;
/* a size of 0xff also means we're done */
if (tpl_link == 0xff)
break;
this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
if (!this)
return -ENOMEM;
for (i = 0; i < tpl_link; i++) {
ret = mmc_io_rw_direct(card, 0, 0,
ptr + i, 0, &this->data[i]);
if (ret)
break;
}
if (ret) {
kfree(this);
break;
}
/* Try to parse the CIS tuple */
ret = cis_tpl_parse(card, func, "CIS",
cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
tpl_code, this->data, tpl_link);
if (ret == -EILSEQ || ret == -ENOENT) {
/*
* The tuple is unknown or known but not parsed.
* Queue the tuple for the function driver.
*/
this->next = NULL;
this->code = tpl_code;
this->size = tpl_link;
*prev = this;
prev = &this->next;
if (ret == -ENOENT) {
/* warn about unknown tuples */
printk(KERN_WARNING "%s: queuing unknown"
" CIS tuple 0x%02x (%u bytes)\n",
mmc_hostname(card->host),
tpl_code, tpl_link);
}
/* keep on analyzing tuples */
ret = 0;
} else {
/*
* We don't need the tuple anymore if it was
* successfully parsed by the SDIO core or if it is
* not going to be queued for a driver.
*/
kfree(this);
}
ptr += tpl_link;
} while (!ret);
/*
* Link in all unknown tuples found in the common CIS so that
* drivers don't have to go digging in two places.
*/
if (func)
*prev = card->tuples;
return ret;
}
12~25行,獲取CIS的地址。保存信息的寄存器地址計算方式:SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i = fn * 0x100 + 0x09 + i,由於地址排列爲little-endian格式(LSB first)所以依次左移拼成一個24位地址。27~30行,之前說過,Common CIS是屬於整張card的(30行),而Function CIS是屬於某個Function的(28行)。由於CIS信息都爲設置,所以*prev指向的內容應該爲NULL(32行)。
34~107行的代碼段即是該函數的核心。結合上面提到的CIS的組織結構,當前ptr指向的一個CIS的code號碼。
42行,code爲0xFF標識一個CIS的結束。
46行,code爲0x00標識當前元組(tuple)沒什麼內容,continue下一個tuple。
49行,當前ptr指向link域,標識當前tuple的數據size,同時也標識下一個tuple的偏移offset。
54行,link爲0xFF同樣標識一個CIS的結束。
57~70行,當前ptr指向數據區域的開始,讀取數據;73~75行,解析數據:
static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
const char *tpl_descr,
const struct cis_tpl *tpl, int tpl_count,
unsigned char code,
const unsigned char *buf, unsigned size)
{
int i, ret;
/* look for a matching code in the table */
for (i = 0; i < tpl_count; i++, tpl++) {
if (tpl->code == code)
break;
}
if (i < tpl_count) {
if (size >= tpl->min_size) {
if (tpl->parse)
ret = tpl->parse(card, func, buf, size);
else
ret = -EILSEQ; /* known tuple, not parsed */
} else {
/* invalid tuple */
ret = -EINVAL;
}
if (ret && ret != -EILSEQ && ret != -ENOENT) {
printk(KERN_ERR "%s: bad %s tuple 0x%02x (%u bytes)\n",
mmc_hostname(card->host), tpl_descr, code, size);
}
} else {
/* unknown tuple */
ret = -ENOENT;
}
return ret;
}
參數tpl、tpl_count爲自己定義的一個cis_tpl類型數組及數組元素個數:typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
const unsigned char *, unsigned);
struct cis_tpl {
unsigned char code;
unsigned char min_size;
tpl_parse_t *parse;
};
/* Known TPL_CODEs table for CIS tuples */
static const struct cis_tpl cis_tpl_list[] = {
{ 0x15, 3, cistpl_vers_1 },
{ 0x20, 4, cistpl_manfid },
{ 0x21, 2, /* cistpl_funcid */ },
{ 0x22, 0, cistpl_funce },
};
cis_tpl的作用是,根據不同的code調用不同的數據解析函數parse。cis_tpl_parse函數根據傳參code進行比對,找出處理該code的解析函數,然後調用之完成解析。以code=20H爲例,調用函數cistpl_manfid解析數據:
static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
const unsigned char *buf, unsigned size)
{
unsigned int vendor, device;
/* TPLMID_MANF */
vendor = buf[0] | (buf[1] << 8);
/* TPLMID_CARD */
device = buf[2] | (buf[3] << 8);
if (func) {
func->vendor = vendor;
func->device = device;
} else {
card->cis.vendor = vendor;
card->cis.device = device;
}
return 0;
}
返回到sdio_read_cis函數,76~104行如果解析成功(else分支)那麼信息已經保存到相應的card或func中,所以可以釋放掉之前malloc的內存;否則就把該信息保存到card或func的tuples指向的內存區域。
106行,ptr指向下一個tuple的code位置,開始下一輪讀取、解析。
113、114行,把所有未知的tuple統一放到card的tuples裏面。