platform_data
按照您在问题中链接的 LWN 文章底部的说明使用。在您的情况下,您的数据结构将如下所示。这显然是未经测试的,但你明白了。您的 platform_data 结构将保存您在定义设备细节的同时设置的函数指针。
int sleep_function_chip1(struct platform_device *pdev)
{
// Go to sleep
return 0;
}
int resume_function_chip1(struct platform_device *pdev)
{
// Resume
return 0;
}
struct my_platform_data {
int (*sleep_function)(struct platform_device *);
int (*resume_function)(struct platform_device *);
};
// Instance of my_platform_data for a particular hardware (called chip1 for now)
static struct my_platform_data my_platform_data_chip1 = {
.sleep_function = &sleep_function_chip1,
.resume_function = &resume_function_chip1,
};
// Second instance of my_platform_data for a different hardware (called chip2 for now)
static struct my_platform_data my_platform_data_chip2 = {
.sleep_function = &sleep_function_chip2,
.resume_function = &resume_function_chip2,
};
// Now include that data when you create the descriptor for
// your platform
static struct platform_device my_platform_device_chip1 = {
.name = "my_device",
.id = 0,
.dev = {
.platform_data = &my_platform_data_chip1,
}
};
int some_driver_function() {
struct platform_device *pdev;
pdev = // wherever you store this
// Any time you need that data, extract the platform_data pointer
struct my_platform_data *pd = (struct my_platform_data*)pdev->dev.platform_data;
// Call the function pointer
pd->sleep_function(pdev);
}