Linux驱动开发之设备驱动模型

您所在的位置:网站首页 linux体系结构风格 Linux驱动开发之设备驱动模型

Linux驱动开发之设备驱动模型

#Linux驱动开发之设备驱动模型| 来源: 网络整理| 查看: 265

概述

Linux的设备驱动模型分为总线、设备和驱动三个实体。这三个实体在内核里的职责分别如下:

总线:负责管理设备和驱动,为设备匹配合适的驱动,为驱动匹配合适的设备。 设备:负责提供硬件资源。 驱动:负责使用设备提供的硬件资源。

总线将设备和驱动联系起来,使得它们之间解耦。

优点

Linux驱动程序模型是先前在内核中使用的所有不同驱动程序模型的统一。它旨在通过将一组数据和操作整合到全局可访问的数据结构中,来扩展基于基础总线来桥接设备驱动程序。这样,不同类型的总线之间就可以有一致性。

统一总线模型包括一组所有总线都具有的公共属性和一组公共回调,如总线探测期间的设备发现、总线关闭、总线电源管理等。通用的设备和桥接接口反映了现代计算机的目标:即执行无缝设备“即插即用”,电源管理和热插拔的能力。

此外,Linux内核可以在各种体系结构和硬件平台上运行,因此需要最大限度地提高代码在平台之间的可重用性。分层实现也实现了软件工程的高内聚-低耦合的设计思想。低耦合体现在对外提供统一的抽象访问接口,高内聚将相关度紧密的集中抽象实现。

platform设备驱动

在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; int id; boo id_auto; struct devicedev; u32 num_resources; struct resource *resource; const struct platform_device_id *id_entry; char *driver_override; /* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell; /* arch specific additions */ struct pdev_archdata archdata; };

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 (*resume)(struct platform_device *); struct device_driver driver; const struct platform_device_id *id_table; bool prevent_deferred_probe; };

直接填充platform_driver的suspend()、resume()做电源管理回调的方法目前已经过时,较好的做法是实现platform_driver的device_driver中的dev_pm_ops结构体成员.

struct device_driver { const char *name; struct bus_type *bus; struct module *owner; const char *mod_name; /* used for built-in modules */ bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 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; };

与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_groups = platform_dev_groups, .match = platform_match, .uevent = platform_uevent, .pm = &platform_dev_pm_ops, };

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

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

从代码中可以看出,匹配platform_device和platform_driver有4种可能性,

基于设备树风格的匹配;

基于ACPI风格的匹配;

匹配ID表(即platform_device设备名是否出现在platform_driver的ID表内);

匹配platform_device设备名和驱动的名字。

platform_driver模板

把字符设备驱动模板改写为platform_dirver驱动模板,这里有一个问题,因为platform_driver需要和platform_device(或设备树)配合使用,否则会匹配(match)不上,xxx_probe函数就不会调用。

#include #include #include #include #include #include #include #include #define DEV_CNT 1 #define DEV_NAME "xxx" /* 设备名 */ struct xxx_dev_t { dev_t devid; struct cdev cdev; struct class* class; struct device* device; int major; int minor; }; static struct xxx_dev_t xxx_dev; static int xxx_open(struct inode* indoe, struct file* filp) { filp->private_data = &xxx_dev; return 0; } static ssize_t xxx_read(struct file* filp, char __user* buf, size_t cnt, loff_t* offt) { return 0; } static ssize_t xxx_write(struct file* filp, const char __user* buf, size_t cnt, loff_t* offt) { return 0; } static int xxx_release(struct inode* inode, struct file* filp) { return 0; } static struct file_operations xxx_fops = { .owner = THIS_MODULE, .open = xxx_open, .read = xxx_read, .write = xxx_write, .release = xxx_release, }; static int xxx_probe(struct platform_device* pdev) { int ret = 0; /* 1、注册字符设备驱动 */ /* 1.1 创建设备号 */ if (xxx_dev.major) { xxx_dev.devid = MKDEV(xxx_dev.major, 0); register_chrdev_region(xxx_dev.devid, DEV_CNT, DEV_NAME); } else { alloc_chrdev_region(&xxx_dev.devid, 0, DEV_CNT, DEV_NAME); xxx_dev.major = MAJOR(xxx_dev.devid); xxx_dev.minor = MINOR(xxx_dev.devid); } printk("xxx chr dev major = %d,minor = %d\n", xxx_dev.major, xxx_dev.minor); /* 1.2 初始化cdev */ xxx_dev.cdev.owner = THIS_MODULE; cdev_init(&xxx_dev.cdev, &xxx_fops); /* 1.3 添加一个cdev */ cdev_add(&xxx_dev.cdev, xxx_dev.devid, DEV_CNT); /* 1.4 创建类 */ xxx_dev.class = class_create(THIS_MODULE, DEV_NAME); if (IS_ERR(xxx_dev.class)) { ret = PTR_ERR(xxx_dev.class); printk("%s: class create failed, ret:%d\n", __func__, ret); goto class_err; } /* 1.5 创建设备 */ xxx_dev.device = device_create(xxx_dev.class, NULL, xxx_dev.devid, NULL, DEV_NAME); if (IS_ERR(xxx_dev.device)) { ret = PTR_ERR(xxx_dev.device); printk("%s: device create failed, ret:%d\n", __func__, ret); goto device_err; } printk("%s: exit()\n", __func__); return 0; device_err: class_destroy(xxx_dev.class); class_err: cdev_del(&xxx_dev.cdev); unregister_chrdev_region(xxx_dev.devid, DEV_CNT); return ret; } static int xxx_remove(struct platform_device* pdev) { /* 注销字符设备驱动 */ device_destroy(xxx_dev.class, xxx_dev.devid); class_destroy(xxx_dev.class); cdev_del(&xxx_dev.cdev); unregister_chrdev_region(xxx_dev.devid, DEV_CNT); return 0; } // clang-format off #ifdef CONFIG_OF static const struct of_device_id xxx_of_matches[] = { { .compatible = "atk,xxx", }, { /* sentinel */} }; #endif // clang-format on static struct platform_driver xxx_driver = { .driver = { .name = "xxx", .owner = THIS_MODULE, .of_match_table = of_match_ptr(xxx_of_matches), }, .probe = xxx_probe, .remove = xxx_remove, }; #if 0 static int __init xxx_init(void) { int ret = 0; platform_driver_register(&xxx_driver); return ret; } static void __exit xxx_exit(void) { platform_driver_unregister(&xxx_driver); } module_init(xxx_init); module_exit(xxx_exit); #else module_platform_driver(xxx_driver); #endif MODULE_LICENSE("GPL"); MODULE_AUTHOR("author");


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3