struct platform_device *pdev = to_platform_device(dev);
struct private_data *pdata = platform_get_drvdata(pdev);
宏to_platform_device(dev)在内核中的位置是include/linux/platform_device.h
#define to_platform_device(x) \
container_of((x), struct platform_device, dev)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type )( (char )__mptr - offsetof(type,member) );})
Linux系统常用如上方法通过结构体成员来查找结构体变量的起始地址
着重理解两点即可:
1)to_platform_device(x) ,x实际上即为:struc_xx.mebxx
2)offsetof(type,member)实际上就是mebxx到struc_xx起始处的偏移量而已
通过如上两点,便可得知 struc_xx地址
附程序:
#include “stdafx.h”
struct student{
char name[20];
char sex;
}stu={“zhangsan”,’m’};
main()
{
struct student *stu_ptr; //存储container_of宏的返回值
int offset; //存储offsetof宏的返回值
//下面三行代码等同于 container_of(&stu.sex,struct student, sex )参数带入的情形
const typeof(((struct student*)0)->sex) *_mptr = &stu.sex;
//首先定义一个 _mptr指针, 类型为struct student结构体中sex成员的类型
//typeof 为获取(((struct student*)0)->sex)的类型,此处此类型为char
//((struct student*)0)在offsetof处讲解
offset = (int)(&((struct student *)0)->sex);
/*((struct student*)0)为 把 0地址 强制转化为指向student结构体类型的指针
该指针从地址 0 开始的 21个字节用来存放name 与 sex(char name〔20〕与 char sex共21字节)
sex存放在第20个字节出(从0字节开始)
&((struct student *)0)->sex 取出sex地址(此处即为20) 并强制转化为整形
所以offset为20,后面的printf结果将证明这一点*/
stu_ptr = (struct student *)((char*)_mptr - offset);
/*((char*)_mptr - offset)此处先把_mptr指针转化为字符形指针
(为什么这么做呢? 如果_mptr为整形指针 _mptr - offset 相当于减去 sizeof(int)*offset个字节)
减去 offset值 相当于 得到_mptr所在结构体的首地址(即stu的地址)
然后我们把 该地址 强制转化为 struct student类型即可正常使用了*/
printf("offsetof stu.sex = %d/n",offset);
printf("stu_ptr->name:%s/tstu_ptr->sex:%c/n", stu_ptr->name, stu_ptr->sex);
return 0;
}