对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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值