platform_set_drvdata(struct platform_device *pdev, void *data)
platform_get_drvdata(const struct platform_device *pdev):
驱动中常用到platform_set_drvdata 和 platform_get_drvdata这两个函数,用于保存局部变量:
include/linux/platform_device.h中:
static inline void *platform_get_drvdata(const struct platform_device *pdev)
{
return dev_get_drvdata(&pdev->dev);
}
{
return dev_get_drvdata(&pdev->dev);
}
static inline void platform_set_drvdata(struct platform_device *pdev, void *data)
{
dev_set_drvdata(&pdev->dev, data);
}
{
dev_set_drvdata(&pdev->dev, data);
}
static inline void
dev_set_drvdata (struct device *dev, void *data)
{
dev->driver_data = data;
}
dev_set_drvdata (struct device *dev, void *data)
{
dev->driver_data = data;
}
就是吧data赋值给dev->driver_data,pdev是平台总线设备,对于整个驱动是可见的,所以可以通过platform_get_drvdata来获取data。
container_of(ptr, type, member)
问题:如何通过结构中的某个变量获取结构本身的指针???
container_of(ptr, type, member)宏的作用是 传入结构体类型type的域member的地址ptr,返回该结构体变量的首地址。
container_of(ptr, type, member)宏的作用是 传入结构体类型type的域member的地址ptr,返回该结构体变量的首地址。
member是结构体类型type的成员,ptr是成员member的实例,返回ptr的入口地址;即通过结构体中一个成员的地址来得到此成员所在结构体的地址。
关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.
关于container_of见kernel.h中:
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ /
const typeof( ((type *)0)->member ) *__mptr = (ptr); /
(type *)( (char *)__mptr - offsetof(type,member) );})
container_of在Linux Kernel中的应用非常广泛,它用于获得某结构中某成员的入口地址.