Linux设备驱动和设备匹配过程

Linux设备驱动和设备匹配过程

在写驱动时我们都会用到一些注册函数比如:platform_driver_register,spi_register_driver、i2c_add_driver,接下来我们就一路追踪看看内核是怎样将我们的驱动和设备匹配起来并且调用我们写的prob函数,在此我们用platform_driver为例子。

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;**
	bool prevent_deferred_probe;
};

struct device_driver {
	const char		*name;  //与设备匹配时会用到的驱动名,一样就匹配成
	struct bus_type		*bus;  在注册函数platform_driver_register中指定

	struct module		*owner;
	const char		*mod_name;	/* used for built-in modules */

	bool suppress_bind_attrs;	/* disables bind/unbind via sysfs */
	enum probe_type probe_type;

	const struct of_device_id	*of_match_table;  //与设备树匹配,名称一样就匹配成功
	const struct acpi_device_id	*acpi_match_table;

	int (*probe) (struct device *dev);  //调用注册函数指定
	int (*remove) (struct device *dev);
	void (*shutdown) (struct device *dev);
	int (*suspend) (struct device *dev, pm_message_t state);
	int (*resume) (struct device *dev);
	const struct attribute_group **groups;

	const struct dev_pm_ops *pm;

	struct driver_private *p;
};

1,简单来说,我们在编写驱动时指定的 struct platform_device_id *id_table、const char *name、const struct of_device_id *of_match_table 将是判断我们的驱动和设备是否能匹配的依据

匹配过程

#define platform_driver_register(drv) \
	__platform_driver_register(drv, THIS_MODULE)
	
---------------------------------------------------------------------
int __platform_driver_register(struct platform_driver *drv,
				struct module *owner)
{
	drv->driver.owner = owner;
	drv->driver.bus = &platform_bus_type;  驱动的总线类型指向platform_bus_type
	drv->driver.probe = platform_drv_probe;
	drv->driver.remove = platform_drv_remove;
	drv->driver.shutdown = platform_drv_shutdown;

	return driver_register(&drv->driver);  //在此函数中进行注册
}

在这个函数中指定了 drv->driver.bus = &platform_bus_type

struct bus_type platform_bus_type = {
	.name		= "platform",
	.dev_groups	= platform_dev_groups,
	.match		= platform_match,  //最后匹配的时候会调用
	.uevent		= platform_uevent,
	.pm		= &platform_dev_pm_ops,
	.force_dma	= true,
};

我们继续来看 driver_register 如何一步步调用到我们的 platform_match 函数


/**
 * driver_register - register driver with bus
 * @drv: driver to register
 *
 * We pass off most of the work to the bus_add_driver() call,
 * since most of the things we have to do deal with the bus
 * structures.
 */
int driver_register(struct device_driver *drv)
{
	int ret;
	struct device_driver *other;

	BUG_ON(!drv->bus->p);

//检测总线的操作函数和驱动的操作函数是否同时存在
	if ((drv->bus->probe && drv->probe) ||
	    (drv->bus->remove && drv->remove) ||
	    (drv->bus->shutdown && drv->shutdown))
		printk(KERN_WARNING "Driver '%s' needs updating - please use "
			"bus_type methods\n", drv->name);
	//查找这个驱动是否已经在总线上注册,并增加引用计数,若已经注册,则返回提示信息

	other = driver_find(drv->name, drv->bus);
	if (other) {
		printk(KERN_ERR "Error: Driver '%s' is already registered, "
			"aborting...\n", drv->name);
		return -EBUSY;
	}
	ret = bus_add_driver(drv);  //如果之前没注册,就在此注册
	if (ret)
		return ret;
	ret = driver_add_groups(drv, drv->groups);
	if (ret) {
		bus_remove_driver(drv);
		return ret;
	}
	kobject_uevent(&drv->p->kobj, KOBJ_ADD);

	return ret;
}

可以看到 driver_register 中调用了 bus_add_driver 完成注册,我们继续跟进去看看


/**
 * bus_add_driver - Add a driver to the bus.
 * @drv: driver.
 */
int bus_add_driver(struct device_driver *drv)
{
	struct bus_type *bus;
	struct driver_private *priv;
	int error = 0;
	//用于增加该bus所属的顶层bus的kobject的引用计数,返回的是其所属的顶层bus的指针。

	bus = bus_get(drv->bus);
	if (!bus)
		return -EINVAL;

	pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv) {
		error = -ENOMEM;
		goto out_put_bus;
	}
	klist_init(&priv->klist_devices, NULL, NULL);
	priv->driver = drv;
	drv->p = priv;
	
	//指向顶层的bus的p->drivers_kset
	//设置私有数据的父容器,在这一步中,设置了kset为platform下的drivers_kset结构,也就是drivers呢个目录 
	priv->kobj.kset = bus->p->drivers_kset;
	error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
				     "%s", drv->name);
	if (error)
		goto out_unregister;

	klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
	if (drv->bus->p->drivers_autoprobe) {//drivers_autoprobe在初始化的时候定义为1,//在我们注册总线bus_register()中,将值置为1.
	//系统则会调用下面的driver_attach函数进行驱动与设备的匹配
		if (driver_allows_async_probing(drv)) {
			pr_debug("bus: '%s': probing driver %s asynchronously\n",
				drv->bus->name, drv->name);
			async_schedule(driver_attach_async, drv);
		} else {
			error = driver_attach(drv);
			if (error)
				goto out_unregister;
		}
	}
	module_add_driver(drv->owner, drv);

	error = driver_create_file(drv, &driver_attr_uevent);
	if (error) {
		printk(KERN_ERR "%s: uevent attr (%s) failed\n",
			__func__, drv->name);
	}
	error = driver_add_groups(drv, bus->drv_groups);
	if (error) {
		/* How the hell do we get out of this pickle? Give up */
		printk(KERN_ERR "%s: driver_create_groups(%s) failed\n",
			__func__, drv->name);
	}

	if (!drv->suppress_bind_attrs) {
		error = add_bind_files(drv);
		if (error) {
			/* Ditto */
			printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
				__func__, drv->name);
		}
	}

	return 0;

out_unregister:
	kobject_put(&priv->kobj);
	/* drv->p is freed in driver_release()  */
	drv->p = NULL;
out_put_bus:
	bus_put(bus);
	return error;
}

代码比较长,但我们要抓住重点,可以看到 bus_add_driver 调用了 driver_attach 来继续完成注册的任务,那我们继续看看这个函数:


/**
 * driver_attach - try to bind driver to devices.
 * @drv: driver.
 *
 * Walk the list of devices that the bus has on it and try to
 * match the driver with each one.  If driver_probe_device()
 * returns 0 and the @dev->driver is set, we've found a
 * compatible pair.
 */
int driver_attach(struct device_driver *drv)
{
	return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
}

跟进bus_for_each_dev可以看到最终调用的是 __driver_attach 
那继续看函数__driver_attach



static int __driver_attach(struct device *dev, void *data)
{
	struct device_driver *drv = data;
	int ret;

	/*
	 * Lock device and try to bind to it. We drop the error
	 * here and always return 0, because we need to keep trying
	 * to bind to devices and some drivers will return an error
	 * simply if it didn't support the device.
	 *
	 * driver_probe_device() will spit a warning if there
	 * is an error.
	 */
	 
	//当设备和驱动的名字不匹配的时候返回的是0,然后就会调用下面的return 0;
	ret = driver_match_device(drv, dev);
	if (ret == 0) {
		/* no match */
		return 0;
	} else if (ret == -EPROBE_DEFER) {
		dev_dbg(dev, "Device match requests probe deferral\n");
		driver_deferred_probe_add(dev);
	} else if (ret < 0) {
		dev_dbg(dev, "Bus failed to match device: %d", ret);
		return ret;
	} /* ret > 0 means positive match */

	if (dev->parent)	/* Needed for USB */
		device_lock(dev->parent);
	device_lock(dev);
	if (!dev->driver)
		driver_probe_device(drv, dev);//调用探测函数进行探测,并且调用platform_driver中的probe函数
	device_unlock(dev);
	if (dev->parent)
		device_unlock(dev->parent);

	return 0;
}


可以看到调用了函数 driver_match_device ,并且下面也做出了判断是否匹配成功,成功就调用了下面的 driver_probe_device 来调用prob函数,我们先来看看函数driver_match_device 黎明就在眼前

static inline int driver_match_device(struct device_driver *drv,
				      struct device *dev)
{
	return drv->bus->match ? drv->bus->match(dev, drv) : 1; //调用match函数,如果没用则默认返回1
}

就这一句,又回到最初的起点,platform_match在最开始调用注册函数时指定的


/**
 * platform_match - bind platform device to platform driver.
 * @dev: device.
 * @drv: driver.
 *
 * Platform device IDs are assumed to be encoded like this:
 * "<name><instance>", where <name> is a short description of the type of
 * device, like "pci" or "floppy", and <instance> is the enumerated
 * instance of the device, like '0' or '42'.  Driver IDs are simply
 * "<name>".  So, extract the <name> from the platform_device structure,
 * and compare it against the name of the driver. Return whether they match
 * or not.
 */
static int platform_match(struct device *dev, struct device_driver *drv)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct platform_driver *pdrv = to_platform_driver(drv);

	/* When driver_override is set, only bind to the matching driver */
	if (pdev->driver_override)
		return !strcmp(pdev->driver_override, drv->name);

	/* Attempt an OF style match first */
	if (of_driver_match_device(dev, drv))
		return 1;

	/* Then try ACPI style match */
	if (acpi_driver_match_device(dev, drv))
		return 1;

	/* Then try to match against the id table */
	if (pdrv->id_table)
		return platform_match_id(pdrv->id_table, pdev) != NULL;

	/* fall-back to driver name match */
	return (strcmp(pdev->name, drv->name) == 0);
}

我们可以看到匹配的顺序:
1、先用设备树中的 compatible 属性和platform_driver中的driver中的 of_match_table 来匹配
2、再用 platform_driver 中的 id_table 中的 name 和 platform_device 中的 name来匹配
3、最后用platform_device中的name和 platform_driver 中的 driver 中的 name来匹配

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值