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

时间:2011-11-18 07:32:13

标签: c struct linux-device-driver i2c

我正在编写一个使用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 },
{ }
};

1 个答案:

答案 0 :(得分:2)

我建议不要再研究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没有类”的演讲。它是一块大小的内存。或者大于保存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内存杀手来分配内存。当内核绝对不能在那时休眠时使用GFP_ATOMIC池 - 非常适合在保持自旋锁时或在中断上下文中使用。

虽然可以同时学习C和内核,但是犯错是一个非常无情的环境。在普通用户空间程序中使用一个迷路指针后,会对该进程进行段错误处理,但是内核中的一个迷路指针会使计算机崩溃或破坏内存,文件系统等。