关于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。
这正是接下来的重点要说的。
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位地址。
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。
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;
}
106行,ptr指向下一个tuple的code位置,开始下一轮读取、解析。
113、114行,把所有未知的tuple统一放到card的tuples里面。