我正在研究I2C内核驱动程序,并希望在新文件夹-/ sys / devices / MySensor中提供sysfs文件界面。但是,当我这样做时,我不知道如何将i2c客户端与新的kobject关联。
因此,当调用我的设备属性函数时,传入的设备对象不允许我检索已注册的i2c客户端。
我声明我的属性如下:
static ssize_t my_sensor_do_something(struct device *dev, struct device_attribute *attr, char *buf)
{
struct i2c_client *client;
struct my_sensor_data *data;
int size = 0;
client = to_i2c_client(dev);
my_sensor_dbgmsg("Client Address:0x%02x\n", client->addr);
data = i2c_get_clientdata(client);
return 0
}
static DEVICE_ATTR(do_something, S_IRUGO, my_sensor_do_something, NULL);
static struct attribute *my_sensor_attributes[] = {
&dev_attr_do_something.attr,
NULL
};
static const struct attribute_group my_sensor_attr_group = {
.attrs = my_sensor_attributes,
};
然后,在我的探测功能中,创建我的子文件夹
struct device *my_dev = root_device_register("my_sensor");
err = sysfs_create_group(&my_dev->kobj, &my_sensor_attr_group);
在/ sys / kernel /中创建了子文件夹和do_something文件,但是,当调用do_something()时,检索I2C客户端的尝试将失败-client-> addr为0,而i2c_get_client_data返回null。
有关信息,i2c设备是在设备树中定义的,我可以将设备属性成功添加到现有文件夹
err = sysfs_create_group(client->dev.kobj, &my_sensor_attr_group);
/sys/bus/i2c/devices/i2c-7/7-004c/
很抱歉,这个问题含糊或缺乏足够的细节。我对此还比较陌生。
在创建新的sysfs文件夹并将其与我注册的i2c客户端相关联时,有人知道我会缺少什么吗?
谢谢
答案 0 :(得分:0)
您可以在初始化时将client
放在my_dev
上。
dev_set_drvdata(my_dev, client);
然后在my_sensor_do_something函数中,使用dev_get_drvdata将client
取出
client = dev_get_drvdata(dev);
完整的示例如下
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/slab.h>
static ssize_t my_sensor_do_something(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct i2c_client *client;
void *data;
client = dev_get_drvdata(dev);
data = i2c_get_clientdata(client);
pr_info("Client Address:0x%02x Data:%p\n", client->addr, data);
return 0;
}
static DEVICE_ATTR(do_something, 0444, my_sensor_do_something, NULL);
static struct attribute *my_sensor_attributes[] = {
&dev_attr_do_something.attr,
NULL
};
static const struct attribute_group my_sensor_attr_group = {
.attrs = my_sensor_attributes,
};
static struct device *my_dev;
static void my_sensor_create(struct i2c_client *client)
{
int err;
my_dev = root_device_register("my_sensor");
dev_set_drvdata(my_dev, client);
err = sysfs_create_group(&my_dev->kobj, &my_sensor_attr_group);
if (err)
pr_info("sysfs_create_group failure.\n");
}
struct test_device {
struct i2c_client *client;
};
static int test_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct test_device *dev;
dev = kzalloc(sizeof(struct test_device), GFP_KERNEL);
if (dev == NULL)
return -ENOMEM;
dev->client = client;
i2c_set_clientdata(client, dev);
my_sensor_create(client);
return 0;
}
static int test_i2c_remove(struct i2c_client *client)
{
struct test_client *dev = i2c_get_clientdata(client);
if (my_dev)
root_device_unregister(my_dev);
kfree(dev);
return 0;
}
static const struct i2c_device_id test_i2c_id[] = {
{"test_i2c_client", 0},
{}
};
static struct i2c_driver test_i2c_driver = {
.driver = { .name = "test_i2c_client", },
.probe = test_i2c_probe,
.remove = test_i2c_remove,
.id_table = test_i2c_id,
};
static int __init test_i2c_init_driver(void)
{
return i2c_add_driver(&test_i2c_driver);
}
static void __exit test_i2c_exit_driver(void)
{
i2c_del_driver(&test_i2c_driver);
}
module_init(test_i2c_init_driver);
module_exit(test_i2c_exit_driver);
MODULE_LICENSE("GPL");