i2c 内核驱动程序 - sysfs kobject 和 i2c_client 之间的绑定
i2c kernel driver - Binding between sysfs kobject and i2c_client
我正在研究 I2C 内核驱动程序,并希望在新文件夹中提供一个 sysfs 文件接口 - /sys/devices/MySensor。但是,当我这样做时,我不知道如何将 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);
子文件夹和 do_something 文件在 /sys/kernel/ 中创建,但是当调用 do_something() 时,尝试检索 I2C 客户端失败 - client->地址为 0 且 i2c_get_client_data returns 为空。
关于信息,i2c 设备在设备树中定义,我可以成功地将设备属性添加到现有文件夹中
err = sysfs_create_group(client->dev.kobj, &my_sensor_attr_group);
/sys/bus/i2c/devices/i2c-7/7-004c/
如果这个问题含糊不清或缺乏足够的细节,我们深表歉意。我对此比较陌生。
有没有人知道我在创建新的 sysfs 文件夹时遗漏了什么,以便将其与我注册的 i2c 客户端相关联?
谢谢
您可以在初始化时将 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");
我正在研究 I2C 内核驱动程序,并希望在新文件夹中提供一个 sysfs 文件接口 - /sys/devices/MySensor。但是,当我这样做时,我不知道如何将 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);
子文件夹和 do_something 文件在 /sys/kernel/ 中创建,但是当调用 do_something() 时,尝试检索 I2C 客户端失败 - client->地址为 0 且 i2c_get_client_data returns 为空。
关于信息,i2c 设备在设备树中定义,我可以成功地将设备属性添加到现有文件夹中
err = sysfs_create_group(client->dev.kobj, &my_sensor_attr_group);
/sys/bus/i2c/devices/i2c-7/7-004c/
如果这个问题含糊不清或缺乏足够的细节,我们深表歉意。我对此比较陌生。
有没有人知道我在创建新的 sysfs 文件夹时遗漏了什么,以便将其与我注册的 i2c 客户端相关联?
谢谢
您可以在初始化时将 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");