对于dev_attr_name, dev_attr_new_device, dev_attr_delete_device使用宏函数 DEVICE_ATTR, 将具体读写操作方法函数进行绑定, 请参考 DEVICE_ATTR宏的用法.
I2c-core中:
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
定义了show_name() 为结构体dev_attr_name 的show方法.
定义了i2c_sysfs_new_device() 为 结构体 dev_attr_new_device的store方法.
定义了i2c_sysfs_delete_device() 为 结构体dev_attr_delete_device的 store 方法.
i2c_sysfs_new_device:
staticssize_ti2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
/*
获取adapter 数据结构
*/
struct i2c_adapter *adap = to_i2c_adapter(dev);
struct i2c_board_info info;
struct i2c_client *client;
....
client = i2c_new_device(adap, &info);
...
}
i2c_sysfs_new_device主要功能是: 获取adapter, 调用 i2c_new_device()
struct i2c_client *i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
struct i2c_client *client;
int status;
...
client->adapter = adap;
client->flags = info->flags;
client->addr = info->addr;
client->irq = info->irq;
..
client->dev.parent = &client->adapter->dev;
client->dev.bus = &i2c_bus_type;
client->dev.type = &i2c_client_type;
client->dev.of_node = info->of_node;
status = device_register(&client->dev);
..
}
i2c_new_device() 主要功能是: 将adapter, irq, i2c_bus_type ,i2c_adapter_type等信息填充到一个 i2c_client中, 最后将i2c_client中的device注册到系统内核中.
