platform_set_drvdata 和 platform_get_drvdata函数分析

分析两个小知识点看看platform_set_drvdata 和 platform_get_drvdata函数是如何保存和获取参数的

直接上代码。

static struct platform_device *s900_link_snd_device;
static struct snd_soc_card snd_soc_s900_atc2603c_link = {
	.name = "s900_link",
	.owner = THIS_MODULE,
	.dai_link = s900_atc2603c_link_dai,
	.num_links = ARRAY_SIZE(s900_atc2603c_link_dai),
	.controls = owl_outpa_controls,
	.num_controls = ARRAY_SIZE(owl_outpa_controls),
};

platform_set_drvdata(s900_link_snd_device,&snd_soc_s900_atc2603c_link);
static inline void platform_set_drvdata(struct platform_device *pdev,
					void *data)
{
	dev_set_drvdata(&pdev->dev, data);//dev_set_drvdata(&s900_link_snd_device->dev, snd_soc_s900_atc2609a_link);  
}
int dev_set_drvdata(struct device *dev, void *data)
{
	int error;

	if (!dev->p) {
		error = device_private_init(dev);
		if (error)
			return error;
	}
	dev->p->driver_data = data;//即为s900_link_snd_device->dev->p->driver_datat = snd_soc_s900_atc2609a_link ;
	return 0;
}
EXPORT_SYMBOL(dev_set_drvdata);

跟踪一下参数传递流程可以看出,platform_set_drvdata函数,最终是将data参数赋值给了platform_device->dev->p->driver_data;

接着我们来看一下platform_get_drvdata函数是如何获取到这个data。

struct snd_soc_card *card = platform_get_drvdata(pdev);//即platform_get_drvdata(s900_link_snd_device);
static inline void *platform_get_drvdata(const struct platform_device *pdev)
{
	return dev_get_drvdata(&pdev->dev); //dev_get_drvdata(&s900_link_snd_device->dev)
}
void *dev_get_drvdata(const struct device *dev)
{
	if (dev && dev->p)
		return dev->p->driver_data;  //return s900_link_snd_device->dev->p->driver_data;
	return NULL;
}
EXPORT_SYMBOL(dev_get_drvdata);
至此就清楚了platform_set_drvdata 和 platform_get_drvdata函数是如何保存和获取参数的。






你可能感兴趣的:(Linux内核笔记)