1. 程式人生 > >platform_driver註冊時probe被呼叫關係

platform_driver註冊時probe被呼叫關係

涉及到的結構體:

struct platform_driver {
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
};
struct platform_device {
	const char	* name;
	int		id;
	struct device	dev;
	u32		num_resources;
	struct resource	* resource;


	const struct platform_device_id	*id_entry;


	/* MFD cell pointer */
	struct mfd_cell *mfd_cell;


	/* arch specific additions */
	struct pdev_archdata	archdata;
};

platform_driver_register(&s3c2410wdt_driver);

註冊一個平臺驅動

int platform_driver_register(struct platform_driver *drv)
{
	drv->driver.bus = &platform_bus_type;
	if (drv->probe)
		drv->driver.probe = platform_drv_probe;
	if (drv->remove)
		drv->driver.remove = platform_drv_remove;
	if (drv->shutdown)
		drv->driver.shutdown = platform_drv_shutdown;

	return driver_register(&drv->driver);
}
當struct platform_driver中有probe函式時,將struct platform中struct driver的probe函式設為platform_drv_probe,這個函式與具體驅動中實現的probe無關。只是在被呼叫時,根據傳入的dev引數去找到相對應的pltform_device和platform_driver,把platform_device傳入然後呼叫platform_driver中的probe函式。

struct platform_driver *drv = to_platform_driver(_dev->driver);
struct platform_device *dev = to_platform_device(_dev);

其中利用的是container_of巨集。

把platform_driver中的driver註冊進去後,如果匹配到device,會執行driver中的probe即platform_drv_probe。