Linux总线设备驱动模型简要分析

在Linux 2.6以后的设备驱动模型中,需关心总线、设备和驱动这3个实体,总线将设备和驱动绑定。在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。

一个现实的Linux设备和驱动通常都需要挂接在一种总线上,对于本身依附于PCI、USB、I2C、SPI等的设备而言,这自然不是问题,但是在嵌入式系统里面,在SoC系统中集成的独立外设控制器、挂接在SoC内存空间的外设等却不依附于此类总线。基于这一背景,Linux发明了一种虚拟的总线,称为platform总线,相应的设备称为platform_device,而驱动称为platform_driver。

注意: 所谓的platform_device并不是与字符设备、块设备和网络设备并列的概念,而是Linux系统提供的一种附加手段,例如,我们通常把在SoC内部集成的I2C、RTC、LCD、看门狗等控制器都归纳为platform_device,而它们本身就是字符设备。platform_device结构体的定义如代码清单如下所示。

struct platform_device {
    const char    * name;
    u32        id;
    struct device    dev;
    u32        num_resources;
    struct resource    * resource;
};

其中有个重要的成员是resource,是设备的资源信息,如IO地址,中断号等。有的设备可能有多个资源,通常使用platform_get_resource函数来获取资源。

platform_driver这个结构体中包含probe()、remove()、一个device_driver实例、电源管理函数suspend()、resume()。 如代码清单如下所示。

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 (*suspend_late)(struct platform_device *, pm_message_t state);
	int (*resume_early)(struct platform_device *);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
};

直接填充platform_driver的suspend()、resume()做电源管理回调的方法目前已经过时,较好的做法是实现platform_driver的device_driver中的dev_pm_ops结构体成员。后续的Linux电源管理章节会对此进行更细致的介绍, 代码清单如下给出了device_driver的定义。

struct device_driver {
	const char		* name;
	struct bus_type		* bus;

	struct kobject		kobj;
	struct klist		klist_devices;
	struct klist_node	knode_bus;

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

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

platform_driver结构体有device_driver成员,该成员的各自字段如上所示,device_driver也有probe、remove、shutdown等函数,在平台驱动注册的时候被初始化。

与platform_driver地位对等的i2c_driver、spi_driver、usb_driver、pci_driver中都包含了device_driver结构体实例成员。它其实描述了各种xxx_driver(xxx是总线名) 在驱动意义上的一些共性。

系统为platform总线定义了一个bus_type的实例platform_bus_type,其定义位于drivers/base/platform.c下, 如代码清单如下所示。

struct bus_type platform_bus_type = {
	.name		= "platform",
	.dev_attrs	= platform_dev_attrs,
	.match		= platform_match,
	.uevent		= platform_uevent,
	.suspend	= platform_suspend,
	.suspend_late	= platform_suspend_late,
	.resume_early	= platform_resume_early,
	.resume		= platform_resume,
};

这里要重点关注其match()成员函数,正是此成员函数确定了platform_device和platform_driver之间是如何进行匹配, 如代码清单下所示。

/* platform_match函数用于匹配总线中的驱动和设备 */  
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);  
  
    /* match against the id table first */  
    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);  
}  

从以上代码清单可以看出,匹配platform_device和platform_driver有4种可能性,一是基于设备树风格的匹配; 二是基于ACPI风格的匹配; 三是匹配ID表(即platform_device设备名是否出现在platform_driver的ID表内);第四种是匹配platform_device设备名和驱动的名字。

platform_match函数首先判断是否有id_table,如果有则使用id_table来进行匹配,否则,判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的probe函数。

平台设备的注册,使用platform_device_register函数:

int platform_device_register(struct platform_device * pdev)
{
	device_initialize(&pdev->dev);
	return platform_device_add(pdev);
}

platform_device_register函数先通过device_initialize函数初始化platform_device的device成员,然后调用platform_device_add向内核添加一个平台设备。

int platform_device_add(struct platform_device *pdev)  
{  
    int i, ret = 0;  
  
    if (!pdev)  /* 如果pdev为空则返回EINVAL */  
        return -EINVAL;  
  
    /* 如果pdev->dev.parent为空则将pdev->dev.parent设置为platform_bus */  
    if (!pdev->dev.parent)  
        pdev->dev.parent = &platform_bus;  
  
    pdev->dev.bus = &platform_bus_type;  /* 设置总线类型 */  
  
    if (pdev->id != -1)      /* 如果id = -1则表示自动分配name */  
        dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);  
    else  
        dev_set_name(&pdev->dev, pdev->name);  
  
    for (i = 0; i < pdev->num_resources; i++) {  
        struct resource *p, *r = &pdev->resource[i]; /* 获取资源 */  
  
        if (r->name == NULL)  
            r->name = dev_name(&pdev->dev);  
  
        p = r->parent;  
        if (!p) {  
            if (resource_type(r) == IORESOURCE_MEM) /* 设置资源类型 */  
                p = &iomem_resource;  
            else if (resource_type(r) == IORESOURCE_IO)  
                p = &ioport_resource;  
        }  
  
        if (p && insert_resource(p, r)) {  
            printk(KERN_ERR  
                   "%s: failed to claim resource %d\n",  
                   dev_name(&pdev->dev), i);  
            ret = -EBUSY;  
            goto failed;  
        }  
    }  
  
    pr_debug("Registering platform device '%s'. Parent at %s\n",  
         dev_name(&pdev->dev), dev_name(pdev->dev.parent));  
  
    /* 向内核添加一个device */  
    ret = device_add(&pdev->dev);  
    if (ret == 0)  
        return ret;  
  
 failed:  
    while (--i >= 0) {  
        struct resource *r = &pdev->resource[i];  
        unsigned long type = resource_type(r);  
  
        if (type == IORESOURCE_MEM || type == IORESOURCE_IO)  
            release_resource(r);  
    }  
  
    return ret;  
}  

platform_device_add最终调用device_add来完成平台设备的注册。

相反地,如果要注销平台设备则使用platform_device_unregister函数

void platform_device_unregister(struct platform_device *pdev)  
{  
    platform_device_del(pdev);  
    platform_device_put(pdev);  
}  

platform_device_unregister函数调用platform_device_del函数来注销平台设备:

void platform_device_del(struct platform_device *pdev)  
{  
    int i;  
  
    if (pdev) {  
        device_del(&pdev->dev);  
  
        for (i = 0; i < pdev->num_resources; i++) {  
            struct resource *r = &pdev->resource[i];  
            unsigned long type = resource_type(r);  
  
            if (type == IORESOURCE_MEM || type == IORESOURCE_IO)  
                release_resource(r);  
        }  
    }  
}  

platform_device_del函数调用device_del函数来删除平台设备,相应地,要释放资源应调用release_resource函数,前提是资源的类型必须为IORESOURCE_MEM或者IORESOURCE_IO

平台驱动的注册使用platform_driver_register函数:

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;  
    if (drv->suspend)  
        drv->driver.suspend = platform_drv_suspend;  
    if (drv->resume)  
        drv->driver.resume = platform_drv_resume;  
    return driver_register(&drv->driver);  
}  

先初始化platform_driver里的driver,该driver的类型为device_driver,设置driver的bus为platform_bus_type;设置driver的probe为platform_drv_probe;设置driver的remove为platform_drv_remove;设置driver的shutdown为platform_drv_shutdown;设置driver的suspend为platform_drv_suspend;设置driver的resume为platform_drv_resume,最后调用driver_register函数来注册平台驱动。

相反地,要注销平台驱动的话,使用platform_driver_unregister函数:

void platform_driver_unregister(struct platform_driver *drv)  
{  
    driver_unregister(&drv->driver);  
}  

附韦老师总结的bus_drv_dev模型的框架图:

 


 

转载于:https://my.oschina.net/cht2000/blog/983830

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值