gpt4 book ai didi

创建 I2C 设备驱动程序结构设置

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:49 24 4
gpt4 key购买 nike

我正在编写一个使用 I2C 与主机通信的设备驱动程序。

下面是我想学习和理解的代码。如果我对下面的代码的理解有误,请帮帮我。 “//”是我自己的评论和我对代码的理解。

// declare there will be a member struct inside the class example_state. This member is pointing to i2c_client.
struct example_state {
struct i2c_client *client;
};

static int example_probe(struct i2c_client *client, const struct i2c_device_id *id{

// declare this to be a local struct inside the example_probe
struct example_state *state;

// get "struct device" to be pointed client's member name dev
// Question: is "struct device *dev" part of "struct i2c_client"?
// if "struct device *dev" imported from somewhere that why there is no need to allocate memory?
struct device *dev = &client->dev;

// allocate a memory space for "struct example_state"
// at this point "struct example_state" is still empty/NULL
// **Question:** but what is the relationship of this local "struct example_state"
// with the one declared before entering struct example_state function?
state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
if (state == NULL) {
dev_err(dev, "failed to create our state\n");
return -ENOMEM;
}

// after memory allocated set the "struct i2c_client" point to "struct example_state"'s member namely "client".
state->client = client;

// set the our device I2C driver information to the host.
// Question: Where to we set our device driver data?
i2c_set_clientdata(client, state);

/* rest of the initialisation goes here. */

dev_info(dev, "example client created\n");

return 0;
}

static int __devexit example_remove(struct i2c_client *client){
// get the loaded value from host, i guess is like unregister
// my device driver information from the host when i exited.
struct example_state *state = i2c_get_clientdata(client);

kfree(state);
return 0;
}

static struct i2c_device_id example_idtable[] = {
{ "example", 0 },
{ }
};

最佳答案

我建议您与其研究 Linux 内核的 i2c 支持,还不如再看看 K&R 2。相反:

// declare there will be a member struct inside the class example_state. This member is pointing to i2c_client.
struct example_state {
struct i2c_client *client;
};

C 没有类。 Linux 内核的 sysfs 抽象引入了内核自己关于什么是“类”的概念——参见 drivers/base/class.c——但它根本没有关系到 C++、Java、C# 或您熟悉的任何其他语言的类。

static int example_probe(struct i2c_client *client, const struct i2c_device_id *id{

// declare this to be a local struct inside the example_probe
struct example_state *state;

这将 state 声明为指向 struct example_state 内存片的指针。 (我通常会在这里使用object这个词,但我不想抵消上一段“C没有类”的言论。它是一 block 大到或大于容纳 struct example_state 的所有成员所需的数量,并且编译器知道检查操作的类型一致性。也许我应该只说“对象”...)

无论如何,在这行代码中只为一个指针预留了足够的内存——而不是结构本身。

// get "struct device" to be pointed client's member name dev
// Question: is "struct device *dev" part of "struct i2c_client"?
// if "struct device *dev" imported from somewhere that why there is no need to allocate memory?
struct device *dev = &client->dev;

这行中的struct device *dev指针 分配了足够的内存,并告诉编译器该指针只会指向struct device 对象。 =&client->dev 查看 client 参数以找到 dev 成员并创建一个别名 以便更容易在此例程中使用。

// allocate a memory space for "struct example_state"
// at this point "struct example_state" is still empty/NULL
// **Question:** but what is the relationship of this local "struct example_state"
// with the one declared before entering struct example_state function?
state = kzalloc(sizeof(struct example_state), GFP_KERNEL);
if (state == NULL) {
dev_err(dev, "failed to create our state\n");
return -ENOMEM;
}

前面的 struct example_state { .. } 是一个类型声明。它仅向编译器指示存储在带有标记 struct example_state 的结构中的成员的名称、大小和类型。它不分配任何内存。 kzalloc() 在这里调用 确实 分配内存,我们的 struct example_state 对象的大小,它请求正常的 GFP_KERNEL 内存分配池被使用。有关可用内存的不同“池”的详细信息,请参见 include/linux/gfp.h。根据传递给内核内存分配器的标志,内存分配可能仅发生在内核被迫执行某种“垃圾收集”——将脏内存缓冲区写回磁盘、丢弃页面缓存、交换进程,甚至可能调用 OOM 内存 killer 来分配内存。 GFP_ATOMIC 池在内核绝对不能在该点休眠时使用——非常适合在持有自旋锁或在中断上下文中使用。

虽然可以同时学习 C 和内核内部知识,但这是一个非常容易犯错误的环境。在普通用户空间程序中跟踪杂散指针会导致进程出现段错误,但在内核中跟踪杂散指针会使机器崩溃或损坏内存、文件系统等。

关于创建 I2C 设备驱动程序结构设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8179145/

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