对platform_driver_probe理解

本文深入解析platform_driver_probe函数,该函数用于在确定设备不可热插拔且已注册的情况下,注册平台设备驱动并立即执行probe操作。文章阐述了其工作原理,包括如何避免不必要的probe调用,以及在设备未找到时的处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * platform_driver_probe - register driver for non-hotpluggable device
 * @drv: platform driver structure
 * @probe: the driver probe routine, probably from an __init section
 *
 * Use this instead of platform_driver_register() when you know the device
 * is not hotpluggable and has already been registered, and you want to
 * remove its run-once probe() infrastructure from memory after the driver
 * has bound to the device.
 *
 * One typical use for this would be with drivers for controllers integrated
 * into system-on-chip processors, where the controller devices have been
 * configured as part of board setup.
 *
 * Returns zero if the driver registered and bound to a device, else returns
 * a negative error code and with the driver not registered.
 */
 //匹配执行probe,没有设备匹配退出注册队列
 //probe只执行platform_driver_probe调用的那次
 //如果设备注册匹配到这个驱动,也不会调用到probe函数
int __init_or_module platform_driver_probe(struct platform_driver *drv,
		int (*probe)(struct platform_device *))
{
	int retval, code;
//在这里把probe函数指向传入的函数
	/* temporary section violation during probe() */
	drv->probe = probe;
	retval = code = platform_driver_register(drv);

	/* Fixup that section violation, being paranoid about code scanning
	 * the list of drivers in order to probe new devices.  Check to see
	 * if the probe was successful, and make sure any forced probes of
	 * new devices fail.
	 */
	spin_lock(&platform_bus_type.p->klist_drivers.k_lock);
//注册好驱动后,把drv->probe清空,指向NULL
//结果就是1、当设备不存在时,不会调用probe函数。
//2、如果是驱动先注册,设备后注册,也不会调用probe,就是说不支持热拔插
//platform_driver_probe可以理解为注册驱动,匹配就调用probe,不存在就以后就再也不会调用
	drv->probe = NULL;
//判断是否有设备匹配,没有就retval = -ENODEV;
	if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
		retval = -ENODEV;
	drv->driver.probe = platform_drv_probe_fail;
	spin_unlock(&platform_bus_type.p->klist_drivers.k_lock);
//没有匹配到设备,就会执行platform_driver_unregister,卸载掉已注册的设备驱动
	if (code != retval)
		platform_driver_unregister(drv);
	return retval;
}

 

在Linux内核中,platform_driver是一种驱动程序的注册方式,用于驱动平台无关的设备。platform_driver的设计思想是为了简化设备驱动的编写,使得驱动程序能够更容易地在不同的硬件平台上进行移植。下面是一个简单的platform_driver使用示例: 1. 定义platform_driver结构体,需要实现probe(探测设备)和remove(移除设备)两个核心函数,以及可能的shutdown(关机时调用)和suspend(挂起设备)等。 ```c #include <linux/module.h> #include <linux/platform_device.h> static int my_driver_probe(struct platform_device *pdev) { // 设备探测代码 return 0; } static int my_driver_remove(struct platform_device *pdev) { // 设备移除代码 return 0; } static struct platform_driver my_driver = { .probe = my_driver_probe, .remove = my_driver_remove, .driver = { .name = "my_driver", .owner = THIS_MODULE, // 可以在这里添加驱动的属性 }, }; module_platform_driver(my_driver); ``` 2. 在probe函数中编写设备初始化的代码,通常包括资源请求(如中断、I/O端口、内存等)和设备数据结构的初始化。 3. 在remove函数中编写设备卸载时的清理代码,包括释放之前申请的资源。 4. 编译驱动模块,加载到内核中运行。 5. 当platform_device注册时,内核会调用platform_driverprobe函数,从而加载驱动。 6. 在设备不再需要时,可以通过卸载platform_device来调用remove函数,完成设备的卸载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值