一:设备模型建立流程图
二:重要函数分析
(1)新适配器加入内核:
int i2c_add_numbered_adapter(struct i2c_adapter *adap)
{
int id;
int status;
{
int id;
int status;
if (adap->nr & ~MAX_ID_MASK)
return -EINVAL;
return -EINVAL;
retry://为i2c_adapter_idr分配内存
if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
return -ENOMEM;
if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
return -ENOMEM;
mutex_lock(&core_lock);
/* "above" here means "above or equal to", sigh;
* we need the "equal to" result to force the result
*///让指针adap关联一个ID,这个ID值从adap->nr开始
status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
if (status == 0 && id != adap->nr) {
status = -EBUSY;//分配的ID值必须与adap->nr相等
idr_remove(&i2c_adapter_idr, id);
}
mutex_unlock(&core_lock);
if (status == -EAGAIN)
goto retry;
/* "above" here means "above or equal to", sigh;
* we need the "equal to" result to force the result
*///让指针adap关联一个ID,这个ID值从adap->nr开始
status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
if (status == 0 && id != adap->nr) {
status = -EBUSY;//分配的ID值必须与adap->nr相等
idr_remove(&i2c_adapter_idr, id);
}
mutex_unlock(&core_lock);
if (status == -EAGAIN)
goto retry;
if (status == 0)//表示ID分配成功。
status = i2c_register_adapter(adap);//注册一个适配器。
return status;
}
status = i2c_register_adapter(adap);//注册一个适配器。
return status;
}
static int i2c_register_adapter(struct i2c_adapter *adap)
{
int res = 0, dummy;
{
int res = 0, dummy;
/* Can't register until after driver model init */
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
mutex_init(&adap->bus_lock);
mutex_init(&adap->clist_lock);
INIT_LIST_HEAD(&adap->clients);
mutex_init(&adap->clist_lock);
INIT_LIST_HEAD(&adap->clients);
mutex_lock(&core_lock);
/* Add the adapter to the driver core.
* If the parent pointer is not set up,
* we add this adapter to the host bus.
*/
if (adap->dev.parent == NULL) {
adap->dev.parent = &platform_bus;
pr_debug("I2C adapter driver [%s] forgot to specify "
"physical device\n", adap->name);
}
* If the parent pointer is not set up,
* we add this adapter to the host bus.
*/
if (adap->dev.parent == NULL) {
adap->dev.parent = &platform_bus;
pr_debug("I2C adapter driver [%s] forgot to specify "
"physical device\n", adap->name);
}
/* Set default timeout to 1 second if not already set */
if (adap->timeout == 0)
adap->timeout = HZ;
//设置适配器的名
dev_set_name(&adap->dev, "i2c-%d", adap->nr);
adap->dev.release = &i2c_adapter_dev_release;
将适配器设备归类,在驱动注册的时候会根据这个类找寻
//这个类中匹配的适配器。
//这个类表示该设备是一个适配器,而adap->class表示该设备支持的设备类型
adap->dev.class = &i2c_adapter_class;
res = device_register(&adap->dev);//注册该设备
//到此适配器设备已被完全注册到了内核。
if (res)
goto out_list;
if (adap->timeout == 0)
adap->timeout = HZ;
//设置适配器的名
dev_set_name(&adap->dev, "i2c-%d", adap->nr);
adap->dev.release = &i2c_adapter_dev_release;
将适配器设备归类,在驱动注册的时候会根据这个类找寻
//这个类中匹配的适配器。
//这个类表示该设备是一个适配器,而adap->class表示该设备支持的设备类型
adap->dev.class = &i2c_adapter_class;
res = device_register(&adap->dev);//注册该设备
//到此适配器设备已被完全注册到了内核。
if (res)
goto out_list;
dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
/* create pre-declared device nodes for new-style drivers */
/*
结构体board_info包含了一些平台信息,和I2C设备地址。该结构
由函数i2c_register_board_info()加到链表__i2c_board_list上。
函数i2c_scan_static_board_info在该链表上找一个满足这样条件的
devinfo->busnum == adapter->nr的结构体,然后根据board_info中的
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
注册一个client(它代表一个具体的设备),并且
client->dev.parent = &client->adapter->dev;最终将将client->dev
挂到链表adapter->dev->p->klist_children上。
函数device_for_each_child就是根据该链表来找出适配器上的每一个client的。
*/
if (adap->nr < __i2c_first_dynamic_bus_num)
i2c_scan_static_board_info(adap);
/*
结构体board_info包含了一些平台信息,和I2C设备地址。该结构
由函数i2c_register_board_info()加到链表__i2c_board_list上。
函数i2c_scan_static_board_info在该链表上找一个满足这样条件的
devinfo->busnum == adapter->nr的结构体,然后根据board_info中的
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
注册一个client(它代表一个具体的设备),并且
client->dev.parent = &client->adapter->dev;最终将将client->dev
挂到链表adapter->dev->p->klist_children上。
函数device_for_each_child就是根据该链表来找出适配器上的每一个client的。
*/
if (adap->nr < __i2c_first_dynamic_bus_num)
i2c_scan_static_board_info(adap);
/* Notify drivers */
/*
寻找每一个总线类型为i2c_bus_type的驱动,这些驱动都被包装为i2c_driver.
如果驱动类型i2c_driver->class和适配器类型adapter->class匹配,并且
驱动支持的设备地址driver->address_data在适配器中没有被占用。就会根据
这个地址注册一个client挂到这个适配器上。
/*
寻找每一个总线类型为i2c_bus_type的驱动,这些驱动都被包装为i2c_driver.
如果驱动类型i2c_driver->class和适配器类型adapter->class匹配,并且
驱动支持的设备地址driver->address_data在适配器中没有被占用。就会根据
这个地址注册一个client挂到这个适配器上。
另一个任务是调用函数driver->attach_adapter,注册一个有设备号的设备
并将这个适配器与这个设备关联。该driver是i2c_driver并不是所有的driver
都有为一个适配器创建设备节点的能力即有函数driver->attach_adapter。。。。
并将这个适配器与这个设备关联。该driver是i2c_driver并不是所有的driver
都有为一个适配器创建设备节点的能力即有函数driver->attach_adapter。。。。
*/
dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
i2c_do_add_adapter);
dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
i2c_do_add_adapter);
out_unlock:
mutex_unlock(&core_lock);
return res;
mutex_unlock(&core_lock);
return res;
out_list:
idr_remove(&i2c_adapter_idr, adap->nr);
goto out_unlock;
}
idr_remove(&i2c_adapter_idr, adap->nr);
goto out_unlock;
}
static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
{
struct i2c_devinfo *devinfo;
{
struct i2c_devinfo *devinfo;
mutex_lock(&__i2c_board_lock);
/*
devinfo被函数i2c_register_board_info挂到链表__i2c_board_list上。
本函数是在链表__i2c_board_list上找一个结构体devinfo,该结构体满足
devinfo->busnum == adapter->nr。
devinfo中包含了地址,flags,type等信息,可用于初始化一个client
并注册到内核。
结构体devinfo中的信息与平台相关。因此该结构的注册应该是在平台
的初始化时进行。
*/
list_for_each_entry(devinfo, &__i2c_board_list, list) {
if (devinfo->busnum == adapter->nr
&& !i2c_new_device(adapter,
&devinfo->board_info))
dev_err(&adapter->dev,
"Can't create device at 0x%02x\n",
devinfo->board_info.addr);
}
mutex_unlock(&__i2c_board_lock);
}
/*
devinfo被函数i2c_register_board_info挂到链表__i2c_board_list上。
本函数是在链表__i2c_board_list上找一个结构体devinfo,该结构体满足
devinfo->busnum == adapter->nr。
devinfo中包含了地址,flags,type等信息,可用于初始化一个client
并注册到内核。
结构体devinfo中的信息与平台相关。因此该结构的注册应该是在平台
的初始化时进行。
*/
list_for_each_entry(devinfo, &__i2c_board_list, list) {
if (devinfo->busnum == adapter->nr
&& !i2c_new_device(adapter,
&devinfo->board_info))
dev_err(&adapter->dev,
"Can't create device at 0x%02x\n",
devinfo->board_info.addr);
}
mutex_unlock(&__i2c_board_lock);
}
(2)一个新的驱动加入内核:
int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
{
int res;
{
int res;
/* Can't register until after driver model init */
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
if (unlikely(WARN_ON(!i2c_bus_type.p)))
return -EAGAIN;
/* new style driver methods can't mix with legacy ones */
/*
"new style" driver:
is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
"legacy"driver:
driver->detach_adapter || driver->detach_client
attach_adapter和detach_adapter;detach_client和attach_client是两对
操作相反的函数,必须成对出现。
这两类驱动的区别就是他们所拥有的函数不一样。
不能出现这两类驱动的混合体,即兼有这两类驱动的标志函数的驱动。
*/
if (is_newstyle_driver(driver)) {
if (driver->detach_adapter || driver->detach_client) {
printk(KERN_WARNING
"i2c-core: driver [%s] is confused\n",
driver->driver.name);
return -EINVAL;
}
}
/*
"new style" driver:
is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
"legacy"driver:
driver->detach_adapter || driver->detach_client
attach_adapter和detach_adapter;detach_client和attach_client是两对
操作相反的函数,必须成对出现。
这两类驱动的区别就是他们所拥有的函数不一样。
不能出现这两类驱动的混合体,即兼有这两类驱动的标志函数的驱动。
*/
if (is_newstyle_driver(driver)) {
if (driver->detach_adapter || driver->detach_client) {
printk(KERN_WARNING
"i2c-core: driver [%s] is confused\n",
driver->driver.name);
return -EINVAL;
}
}
/* add the driver to the list of i2c drivers in the driver core */
driver->driver.owner = owner;
driver->driver.bus = &i2c_bus_type;//设置它的总线类型
driver->driver.owner = owner;
driver->driver.bus = &i2c_bus_type;//设置它的总线类型
/* for new style drivers, when registration returns the driver core
* will have called probe() for all matching-but-unbound devices.
*/
res = driver_register(&driver->driver);//注册驱动
if (res)
return res;
* will have called probe() for all matching-but-unbound devices.
*/
res = driver_register(&driver->driver);//注册驱动
if (res)
return res;
/*
到这里该驱动已被完全注册到了内核。它可以去匹配内核中已存在的
client而后执行probe函数了。
但是一个i2c_driver还有着其他的使命。如果该驱动是一个携带地址信息的
"new style" driver,则它要把这些每一个地址对应一个client注册到内核。
如果是一个"legacy"driver,则它要用它的driver->attach_adapter函数
去处理每一个adapter->nr空闲的适配器。
每一个函数driver->attach_adapter的操作都跟adapter->nr有关,在这些函数
中都会对adapter->nr进行判断,所以driver->attach_adapter并不是,一个驱动
的函数attach_adapter对每一个适配器都会成功执行。
到这里该驱动已被完全注册到了内核。它可以去匹配内核中已存在的
client而后执行probe函数了。
但是一个i2c_driver还有着其他的使命。如果该驱动是一个携带地址信息的
"new style" driver,则它要把这些每一个地址对应一个client注册到内核。
如果是一个"legacy"driver,则它要用它的driver->attach_adapter函数
去处理每一个adapter->nr空闲的适配器。
每一个函数driver->attach_adapter的操作都跟adapter->nr有关,在这些函数
中都会对adapter->nr进行判断,所以driver->attach_adapter并不是,一个驱动
的函数attach_adapter对每一个适配器都会成功执行。
比如在驱动i2cdev_driver中函数attach_adapter的执行会创建一个设备节点,
但是对同一个适配器两次执行函数attach_adapter就不会产生两个设备节点,
因为产生设备节点的次设备号是由adapter->nr来决定的。内核是不会允许
对同一个设备号注册两次的。
*/
mutex_lock(&core_lock);
pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
INIT_LIST_HEAD(&driver->clients);//初始化链表头用于挂接client
/* Walk the adapters that are already present */
/*
寻找类型为i2c_adapter_class的所有设备,即适配器。
如果驱动为"new style" driver将在函数__attach_adapter中
执行函数i2c_detect,如果当前找到的适配器所支持的设备类型
与驱动所支持的设备类型相匹配,就将驱动所携带的地址信息
driver->address_data,对应着client注册到内核,并插入该适配器。
适配器所支持的设备类型由adapter->class决定,而不是
adapter->dev->class,adapter->dev->class的类型应为i2c_adapter_class。
驱动所支持的设备类型为driver->class。
如果该驱动为"legacy"driver,则在函数__attach_adapter中将执行
函数driver->attach_adapter,将在类i2c_adapter_class上找到的
所有适配器都执行一遍本驱动的driver->attach_adapter函数。
当然并不是所有驱动都能成功执行该函数。该函数的执行会判断
adapter->nr。
*/
class_for_each_device(&i2c_adapter_class, NULL, driver,
__attach_adapter);
/* Walk the adapters that are already present */
/*
寻找类型为i2c_adapter_class的所有设备,即适配器。
如果驱动为"new style" driver将在函数__attach_adapter中
执行函数i2c_detect,如果当前找到的适配器所支持的设备类型
与驱动所支持的设备类型相匹配,就将驱动所携带的地址信息
driver->address_data,对应着client注册到内核,并插入该适配器。
适配器所支持的设备类型由adapter->class决定,而不是
adapter->dev->class,adapter->dev->class的类型应为i2c_adapter_class。
驱动所支持的设备类型为driver->class。
如果该驱动为"legacy"driver,则在函数__attach_adapter中将执行
函数driver->attach_adapter,将在类i2c_adapter_class上找到的
所有适配器都执行一遍本驱动的driver->attach_adapter函数。
当然并不是所有驱动都能成功执行该函数。该函数的执行会判断
adapter->nr。
*/
class_for_each_device(&i2c_adapter_class, NULL, driver,
__attach_adapter);
mutex_unlock(&core_lock);
return 0;
}
return 0;
}
(3)适配器与设备进行类型匹配,探测驱动中的可用地址,并将地址对应于一个client注册到内核
在适配器设备和驱动注册到内核后调用了两个相似的函数如下:
在适配器设备注册到内核后调用函数:
static int i2c_do_add_adapter(struct device_driver *d, void *data)
{
struct i2c_driver *driver = to_i2c_driver(d);
struct i2c_adapter *adap = data;
{
struct i2c_driver *driver = to_i2c_driver(d);
struct i2c_adapter *adap = data;
/* Detect supported devices on that bus, and instantiate them */
//如果驱动的类与适配器的类相匹配,则将驱动支持的设备插入适配器。
i2c_detect(adap, driver);
//如果驱动的类与适配器的类相匹配,则将驱动支持的设备插入适配器。
i2c_detect(adap, driver);
/* Let legacy drivers scan this bus for matching devices */
if (driver->attach_adapter) {//使适配器关联一个设备号或者。。。。。。
/* We ignore the return code; if it fails, too bad */
driver->attach_adapter(adap);
}
return 0;
}
if (driver->attach_adapter) {//使适配器关联一个设备号或者。。。。。。
/* We ignore the return code; if it fails, too bad */
driver->attach_adapter(adap);
}
return 0;
}
驱动注册到内核后调用函数:
static int __attach_adapter(struct device *dev, void *data)
{
struct i2c_adapter *adapter = to_i2c_adapter(dev);
struct i2c_driver *driver = data;
{
struct i2c_adapter *adapter = to_i2c_adapter(dev);
struct i2c_driver *driver = data;
//如果驱动的类与适配器的类相匹配,则将驱动支持的设备插入适配器。
i2c_detect(adapter, driver);
/* Legacy drivers scan i2c busses directly */
if (driver->attach_adapter)//使适配器关联一个设备号或者。。。。。。
driver->attach_adapter(adapter);
if (driver->attach_adapter)//使适配器关联一个设备号或者。。。。。。
driver->attach_adapter(adapter);
return 0;
}
}
两函数的对比:
static int __attach_adapter(struct device *dev, void *data)
与
static int i2c_do_add_adapter(struct device_driver *d, void *data)
前者是在新的驱动加入内核时调用函数class_for_each_device时调用的函数。
函数class_for_each_device是在类i2c_adapter_class中找一个适配器设备
与新驱动匹配。该函数变化的是设备不变的是加入的新的驱动(void *data)
后者是在新的适配器加入内核时调用函数 bus_for_each_drv时调用的函数。
函数bus_for_each_drv是在总线类型为i2c_bus_type的驱动中找到一个驱动
与新加入的适配器匹配。该函数变化的是驱动,不变的是新加入的适配器
(void *data)。
相同点都是执行函数i2c_detect和函数driver->attach_adapter。
其实在bus_for_each_drv和class_for_each_device并不执行匹配的操作。
只是简单的取出所有的驱动或是适配器,执行函数i2c_detect和attach_adapter。
真正的匹配是在函数i2c_detect和attach_adapter中进行的。
函数i2c_detect中会去判断驱动支持的设备类和适配器所支持的设备类
是否匹配。
函数driver->attach_adapter中会判断adapter->nr是不是自己需要处理的
适配器,或者是是不是该适配器已被处理过了。
当然函数i2c_detect和函数driver->attach_adapter是绝不会同时执行的。
因为函数i2c_detect的执行需要用到函数driver->detect而该函数是
"new style" drivers的标志函数。而函数driver->attach_adapter是
"legacy" drivers的标志函数。i2c_driver中是没有兼有这两种驱动的混合体
驱动的。
static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
{
const struct i2c_client_address_data *address_data;
struct i2c_client *temp_client;
int i, err = 0;
int adap_id = i2c_adapter_id(adapter);
{
const struct i2c_client_address_data *address_data;
struct i2c_client *temp_client;
int i, err = 0;
int adap_id = i2c_adapter_id(adapter);
address_data = driver->address_data;
/*
因为注册一个client要调用函数i2c_new_device(adapter, &info);
该函数要用结构体info为client提供必要的信息。所以函数
driver->detect的主要作用是填充结构体info。
client代表一个具体设备而每个设备都有它的地址address_data。
所以driver->detect和address_data都是必不可少。
*/
if (!driver->detect || !address_data)
return 0;
/*
因为注册一个client要调用函数i2c_new_device(adapter, &info);
该函数要用结构体info为client提供必要的信息。所以函数
driver->detect的主要作用是填充结构体info。
client代表一个具体设备而每个设备都有它的地址address_data。
所以driver->detect和address_data都是必不可少。
*/
if (!driver->detect || !address_data)
return 0;
/* Set up a temporary client to help detect callback */
/*
分配一个临时的temp_client,因为temp_client得设备并没有被注册,
也没有被插入适配器,它只是暂存了一些client信息。
*/
temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
if (!temp_client)
return -ENOMEM;
temp_client->adapter = adapter;
/*
分配一个临时的temp_client,因为temp_client得设备并没有被注册,
也没有被插入适配器,它只是暂存了一些client信息。
*/
temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
if (!temp_client)
return -ENOMEM;
temp_client->adapter = adapter;
/* Force entries are done first, and are not affected by ignore
entries */
/*
为什么是forces?
因为跳过了适配器支持的设备类和驱动支持的设备类匹配判断
if (!(adapter->class & driver->class))
goto exit_free;
也就是说即使不匹配,这些设备地址只要满足某些条件也要将其插入适配器。
*/
if (address_data->forces) {
const unsigned short * const *forces = address_data->forces;
int kind;
entries */
/*
为什么是forces?
因为跳过了适配器支持的设备类和驱动支持的设备类匹配判断
if (!(adapter->class & driver->class))
goto exit_free;
也就是说即使不匹配,这些设备地址只要满足某些条件也要将其插入适配器。
*/
if (address_data->forces) {
const unsigned short * const *forces = address_data->forces;
int kind;
for (kind = 0; forces[kind]; kind++) {
for (i = 0; forces[kind][i] != I2C_CLIENT_END;
i += 2) {//adap_id即是分配的与结构体adapter相关联的ID
if (forces[kind][i] == adap_id
|| forces[kind][i] == ANY_I2C_BUS) {
dev_dbg(&adapter->dev, "found force "
"parameter for adapter %d, "
"addr 0x%02x, kind %d\n",
adap_id, forces[kind][i + 1],
kind);
temp_client->addr = forces[kind][i + 1];
//将这个地址的设备插入适配器。
err = i2c_detect_address(temp_client,
kind, driver);
if (err)
goto exit_free;
}
}
}
}
for (i = 0; forces[kind][i] != I2C_CLIENT_END;
i += 2) {//adap_id即是分配的与结构体adapter相关联的ID
if (forces[kind][i] == adap_id
|| forces[kind][i] == ANY_I2C_BUS) {
dev_dbg(&adapter->dev, "found force "
"parameter for adapter %d, "
"addr 0x%02x, kind %d\n",
adap_id, forces[kind][i + 1],
kind);
temp_client->addr = forces[kind][i + 1];
//将这个地址的设备插入适配器。
err = i2c_detect_address(temp_client,
kind, driver);
if (err)
goto exit_free;
}
}
}
}
/* Stop here if the classes do not match */
//适配器和驱动的类型要匹配此处的驱动为i2c_driver
if (!(adapter->class & driver->class))
goto exit_free;
//适配器和驱动的类型要匹配此处的驱动为i2c_driver
if (!(adapter->class & driver->class))
goto exit_free;
/* Stop here if we can't use SMBUS_QUICK */
//如果该适配器不支持I2C_FUNC_SMBUS_QUICK功能则就此结束。
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
if (address_data->probe[0] == I2C_CLIENT_END
&& address_data->normal_i2c[0] == I2C_CLIENT_END)
goto exit_free;
//如果该适配器不支持I2C_FUNC_SMBUS_QUICK功能则就此结束。
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
if (address_data->probe[0] == I2C_CLIENT_END
&& address_data->normal_i2c[0] == I2C_CLIENT_END)
goto exit_free;
dev_warn(&adapter->dev, "SMBus Quick command not supported, "
"can't probe for chips\n");
err = -EOPNOTSUPP;
goto exit_free;
}
"can't probe for chips\n");
err = -EOPNOTSUPP;
goto exit_free;
}
/* Probe entries are done second, and are not affected by ignore
entries either */
//address_data->probe支持的设备地址不受
//address_data->ignore(忽略某些地址)的影响
//而address_data->normal_i2c[i]中的地址就要受到ignore的影响
entries either */
//address_data->probe支持的设备地址不受
//address_data->ignore(忽略某些地址)的影响
//而address_data->normal_i2c[i]中的地址就要受到ignore的影响
for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
if (address_data->probe[i] == adap_id
|| address_data->probe[i] == ANY_I2C_BUS) {
dev_dbg(&adapter->dev, "found probe parameter for "
"adapter %d, addr 0x%02x\n", adap_id,
address_data->probe[i + 1]);
temp_client->addr = address_data->probe[i + 1];
//将这个地址对应的设备插入适配器。
err = i2c_detect_address(temp_client, -1, driver);
if (err)
goto exit_free;
}
}
if (address_data->probe[i] == adap_id
|| address_data->probe[i] == ANY_I2C_BUS) {
dev_dbg(&adapter->dev, "found probe parameter for "
"adapter %d, addr 0x%02x\n", adap_id,
address_data->probe[i + 1]);
temp_client->addr = address_data->probe[i + 1];
//将这个地址对应的设备插入适配器。
err = i2c_detect_address(temp_client, -1, driver);
if (err)
goto exit_free;
}
}
/* Normal entries are done last, unless shadowed by an ignore entry */
//驱动中所支持的地址一般都存于address_data->normal_i2c中
for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
int j, ignore;
//驱动中所支持的地址一般都存于address_data->normal_i2c中
for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
int j, ignore;
ignore = 0;
for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
j += 2) {
if ((address_data->ignore[j] == adap_id ||
address_data->ignore[j] == ANY_I2C_BUS)
&& address_data->ignore[j + 1]
== address_data->normal_i2c[i]) {
dev_dbg(&adapter->dev, "found ignore "
"parameter for adapter %d, "
"addr 0x%02x\n", adap_id,
address_data->ignore[j + 1]);
ignore = 1;
break;
}
}
if (ignore)
continue;
for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
j += 2) {
if ((address_data->ignore[j] == adap_id ||
address_data->ignore[j] == ANY_I2C_BUS)
&& address_data->ignore[j + 1]
== address_data->normal_i2c[i]) {
dev_dbg(&adapter->dev, "found ignore "
"parameter for adapter %d, "
"addr 0x%02x\n", adap_id,
address_data->ignore[j + 1]);
ignore = 1;
break;
}
}
if (ignore)
continue;
dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
"addr 0x%02x\n", adap_id,
address_data->normal_i2c[i]);
temp_client->addr = address_data->normal_i2c[i];
//将这个地址对应的设备插入适配器。
err = i2c_detect_address(temp_client, -1, driver);
if (err)
goto exit_free;
}
"addr 0x%02x\n", adap_id,
address_data->normal_i2c[i]);
temp_client->addr = address_data->normal_i2c[i];
//将这个地址对应的设备插入适配器。
err = i2c_detect_address(temp_client, -1, driver);
if (err)
goto exit_free;
}
exit_free:
kfree(temp_client);
return err;
}
kfree(temp_client);
return err;
}
static int i2c_detect_address(struct i2c_client *temp_client, int kind,
struct i2c_driver *driver)
{
struct i2c_board_info info;
struct i2c_adapter *adapter = temp_client->adapter;
int addr = temp_client->addr;
int err;
//有效地设备地址必须在这个范围内
/* Make sure the address is valid */
if (addr < 0x03 || addr > 0x77) {
dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
addr);
return -EINVAL;
}
struct i2c_driver *driver)
{
struct i2c_board_info info;
struct i2c_adapter *adapter = temp_client->adapter;
int addr = temp_client->addr;
int err;
//有效地设备地址必须在这个范围内
/* Make sure the address is valid */
if (addr < 0x03 || addr > 0x77) {
dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
addr);
return -EINVAL;
}
/* Skip if already in use */
//检测这个地址在适配器adapter中是否被占用。
if (i2c_check_addr(adapter, addr))
return 0;
//检测这个地址在适配器adapter中是否被占用。
if (i2c_check_addr(adapter, addr))
return 0;
/* Make sure there is something at this address, unless forced */
if (kind < 0) {
if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
I2C_SMBUS_QUICK, NULL) < 0)
return 0;
if (kind < 0) {
if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
I2C_SMBUS_QUICK, NULL) < 0)
return 0;
/* prevent 24RF08 corruption */
if ((addr & ~0x0f) == 0x50)
i2c_smbus_xfer(adapter, addr, 0, 0, 0,
I2C_SMBUS_QUICK, NULL);
}
if ((addr & ~0x0f) == 0x50)
i2c_smbus_xfer(adapter, addr, 0, 0, 0,
I2C_SMBUS_QUICK, NULL);
}
/* Finally call the custom detection function */
//清空构体info准备存放信息
memset(&info, 0, sizeof(struct i2c_board_info));
info.addr = addr;
//该函数为结构体info填充一些信息。
err = driver->detect(temp_client, kind, &info);
if (err) {
/* -ENODEV is returned if the detection fails. We catch it
here as this isn't an error. */
return err == -ENODEV ? 0 : err;
}
//清空构体info准备存放信息
memset(&info, 0, sizeof(struct i2c_board_info));
info.addr = addr;
//该函数为结构体info填充一些信息。
err = driver->detect(temp_client, kind, &info);
if (err) {
/* -ENODEV is returned if the detection fails. We catch it
here as this isn't an error. */
return err == -ENODEV ? 0 : err;
}
/* Consistency check */
/*
info.type在函数driver->detect中将把driver->name拷给它。
info.type最终将在函数i2c_new_device中
strlcpy(client->name, info->type, sizeof(client->name));
client代表一个具体的设备,驱动对应着设备,当驱动与设备
相匹配时执行初始化函数函数probe。
在总线i2c_bus_type上驱动与设备匹配的条件是,name相同。
*/
if (info.type[0] == '\0') {
dev_err(&adapter->dev, "%s detection function provided "
"no name for 0x%x\n", driver->driver.name,
addr);
} else {
struct i2c_client *client;
/*
info.type在函数driver->detect中将把driver->name拷给它。
info.type最终将在函数i2c_new_device中
strlcpy(client->name, info->type, sizeof(client->name));
client代表一个具体的设备,驱动对应着设备,当驱动与设备
相匹配时执行初始化函数函数probe。
在总线i2c_bus_type上驱动与设备匹配的条件是,name相同。
*/
if (info.type[0] == '\0') {
dev_err(&adapter->dev, "%s detection function provided "
"no name for 0x%x\n", driver->driver.name,
addr);
} else {
struct i2c_client *client;
/* Detection succeeded, instantiate the device */
dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
info.type, info.addr);
//获取了足够的建立client的信息(info)则注册client,并插入适配器
client = i2c_new_device(adapter, &info);
if (client)//这些设备都是driver所支持的,所以要挂到driver->clients上。
list_add_tail(&client->detected, &driver->clients);
else
dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
info.type, info.addr);
}
return 0;
}
dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
info.type, info.addr);
//获取了足够的建立client的信息(info)则注册client,并插入适配器
client = i2c_new_device(adapter, &info);
if (client)//这些设备都是driver所支持的,所以要挂到driver->clients上。
list_add_tail(&client->detected, &driver->clients);
else
dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
info.type, info.addr);
}
return 0;
}
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *client;
int status;
//将结构体info中存储的各种信息用来初始化client。
//函数driver->detect(temp_client, kind, &info);
//是用来填充info中的值的,由此可见该函数的重要性。
client = kzalloc(sizeof *client, GFP_KERNEL);
if (!client)
return NULL;
//
client->adapter = adap;
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *client;
int status;
//将结构体info中存储的各种信息用来初始化client。
//函数driver->detect(temp_client, kind, &info);
//是用来填充info中的值的,由此可见该函数的重要性。
client = kzalloc(sizeof *client, GFP_KERNEL);
if (!client)
return NULL;
//
client->adapter = adap;
client->dev.platform_data = info->platform_data;
if (info->archdata)
client->dev.archdata = *info->archdata;
client->dev.archdata = *info->archdata;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
//为结构体client命名该名来至携带client地址信息的驱动
strlcpy(client->name, info->type, sizeof(client->name));
client->addr = info->addr;
client->irq = info->irq;
//为结构体client命名该名来至携带client地址信息的驱动
strlcpy(client->name, info->type, sizeof(client->name));
/* a new style driver may be bound to this device when we
* return from this function, or any later moment (e.g. maybe
* hotplugging will load the driver module). and the device
* refcount model is the standard driver model one.
*/
//device_register(&client->dev);注册设备并
//client->dev.parent = &client->adapter->dev;
//以上设置很重要是函数device_for_each_child实现的基础。
status = i2c_attach_client(client);
if (status < 0) {
kfree(client);
client = NULL;
}
return client;
}
* return from this function, or any later moment (e.g. maybe
* hotplugging will load the driver module). and the device
* refcount model is the standard driver model one.
*/
//device_register(&client->dev);注册设备并
//client->dev.parent = &client->adapter->dev;
//以上设置很重要是函数device_for_each_child实现的基础。
status = i2c_attach_client(client);
if (status < 0) {
kfree(client);
client = NULL;
}
return client;
}
int i2c_attach_client(struct i2c_client *client)
{
struct i2c_adapter *adapter = client->adapter;
int res;
{
struct i2c_adapter *adapter = client->adapter;
int res;
/* Check for address business */
//检测地址client->addr在适配器adapter中是否已被占据
res = i2c_check_addr(adapter, client->addr);
if (res)
return res;
//该适配器是所有它支持的设备的父设备
client->dev.parent = &client->adapter->dev;
client->dev.bus = &i2c_bus_type;
//检测地址client->addr在适配器adapter中是否已被占据
res = i2c_check_addr(adapter, client->addr);
if (res)
return res;
//该适配器是所有它支持的设备的父设备
client->dev.parent = &client->adapter->dev;
client->dev.bus = &i2c_bus_type;
if (client->driver)
client->dev.driver = &client->driver->driver;
/*
有两种驱动new-style drivers 和"legacy" drivers前者只注册
client,匹配相应驱动,执行函数probe()。后者是添加适配器
创建设备节点。
*/
if (client->driver && !is_newstyle_driver(client->driver)) {
/*
在函数i2c_client_release中调用complete(&client->released);
通知完成released。在函数i2c_detach_client中有
wait_for_completion(&client->released);
*/
client->dev.release = i2c_client_release;
dev_set_uevent_suppress(&client->dev, 1);
} else
client->dev.release = i2c_client_dev_release;
//注意client->dev->name与client->name的区分
dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
client->addr);
res = device_register(&client->dev);//注册设备
if (res)
goto out_err;
client->dev.driver = &client->driver->driver;
/*
有两种驱动new-style drivers 和"legacy" drivers前者只注册
client,匹配相应驱动,执行函数probe()。后者是添加适配器
创建设备节点。
*/
if (client->driver && !is_newstyle_driver(client->driver)) {
/*
在函数i2c_client_release中调用complete(&client->released);
通知完成released。在函数i2c_detach_client中有
wait_for_completion(&client->released);
*/
client->dev.release = i2c_client_release;
dev_set_uevent_suppress(&client->dev, 1);
} else
client->dev.release = i2c_client_dev_release;
//注意client->dev->name与client->name的区分
dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adapter),
client->addr);
res = device_register(&client->dev);//注册设备
if (res)
goto out_err;
mutex_lock(&adapter->clist_lock);
//将适配器支持的设备插入适配器
list_add_tail(&client->list, &adapter->clients);
mutex_unlock(&adapter->clist_lock);
//将适配器支持的设备插入适配器
list_add_tail(&client->list, &adapter->clients);
mutex_unlock(&adapter->clist_lock);
dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
client->name, dev_name(&client->dev));
//好像该做的都做完了,函数adapter->client_register或许
//在某些特定的场合有着它特定的作用
if (adapter->client_register) {
if (adapter->client_register(client)) {
dev_dbg(&adapter->dev, "client_register "
"failed for client [%s] at 0x%02x\n",
client->name, client->addr);
}
}
client->name, dev_name(&client->dev));
//好像该做的都做完了,函数adapter->client_register或许
//在某些特定的场合有着它特定的作用
if (adapter->client_register) {
if (adapter->client_register(client)) {
dev_dbg(&adapter->dev, "client_register "
"failed for client [%s] at 0x%02x\n",
client->name, client->addr);
}
}
return 0;
out_err:
dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
"(%d)\n", client->name, client->addr, res);
return res;
}
dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
"(%d)\n", client->name, client->addr, res);
return res;
}