Linux字符设备驱动中container_of宏的作用

来源:互联网 发布:会计网络培训 编辑:程序博客网 时间:2024/06/06 03:07

Linux字符设备驱动中container_of宏的作用


首先看看这个宏的原型:

container_of(ptr,type,member)

  功能:根据一个结构体变量中的一个成员变量的指针来获取指向整个结构体变量的指针。

参数:

          ptr:已知的结构体成员的首地址(指针);

          type:要获取的结构体变量的类型

          member:要获取的结构体变量中成员的名字,而不是类型

返回值:要获取的结构体变量的首地址

驱动中常用的用法:

struct hello_device

{

          struct cdev cdev;

          char  data [128];

}hello_device[2];

 

hello_device[0]   //对应 设备0

hello_device[1]   //对应 设备1

int hello_open(struct inode * inode,struct file * file)

  {

 struct hello_device * dev = container_of(inode->i_cdev,struct hello_device,cdev);

file->private_data = dev;

   

ssize_t hello_read(struct file*,char __user *buff,size_t count,loff_t*)

    {

        struct hello_device * dev = file->private_data;

        //dev指向打开的设备结构体

        copy_to_user(buff,dev->data,count);

     }

当我们成功insmod一个设备驱动的时候,我们会通过mknod创建一个设备节点和一个具体的设备(驱动)关联,这个节点就对应这个inode结构体的一个实例,这个实例有一个struct cdev类型的字段i_cdev,它会指向hello_device中的cdev结构体(初始化cdev时候完成的)。最终获取到hello_device结构体的首地址,其cdev已经被实例化,对应一个具体的设备。

所以我们可以根据dev->cdev.dev就可以获得当前这个设备的设备号。然后包含具体设备信息的dev结构体就可以通过file中的private_data成员在open,read,write,ioctl之间传递。


0 0