gpt4 book ai didi

linux - i2c 内核驱动程序 - sysfs kobject 和 i2c_client 之间的绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:30 25 4
gpt4 key购买 nike

我正在研究 I2C 内核驱动程序,并希望在新文件夹 -/sys/devices/MySensor 中提供一个 sysfs 文件接口(interface)。然而,当我这样做时,我不知道如何将 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->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 客户端相关联?

谢谢

最佳答案

您可以在初始化时将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");

关于linux - i2c 内核驱动程序 - sysfs kobject 和 i2c_client 之间的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com