platform_device和platform_driver

关于platform_device和platform_driver的匹配 说句老实话,我不是太喜欢现在Linux 2.6这套bus, platform, device,device driver 的模式。我觉得这种模式破坏了“Linux简单就是美”的哲学,原来那套驱动已经可以干很多事,而且也可以直接注册驱动文件等等,以前的驱动在现在的结构上也还可以使用,把它在注册到bus这棵树上又有什么用呢?虽然可以看到一点对于移植性和平台管理方面的有点,但是我认为现在这种驱动编程的风格越来越像Windows的风格,很不直观和简约。牢骚发完了,bus结构还得继续,说说platform_devic 阅读详情
 
首先介绍一下注册一个驱动的步骤:
1、定义一个platform_driver结构
2、初始化这个结构,指定其probe、remove等函数,并初始化其中的driver变量
3、实现其probe、remove等函数

看platform_driver结构,定义于include/linux/platform_device.h文件中:

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

 
    可见,它包含了设备操作的几个功能函数,同样重要的是,它还包含了一个device_driver结构。刚才提到了驱动程序中需要初始化这个变量。下面看一下这个变量的定义,位于include/linux/device.h中:
 

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

    需要注意这两个变量:name和owner。那么的作用主要是为了和相关的platform_device关联起来,owner的作用是说明模块的所有者,驱动程序中一般初始化为THIS_MODULE。

    下面是一个platform_driver的初始化实例:

static struct platform_driver s3c2410iis_driver = {
  .probe = s3c2410iis_probe,
  .remove = s3c2410iis_remove,
  .driver = {
    .name = "s3c2410-iis",
    .owner = THIS_MODULE,
  },
};

    上面的初始化是一个音频驱动的实例。注意其中的driver这个结构体,只初始化了其name和owner两个量。接着看一下和driver相关的另一个结构,定义如下:

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

   该结构中也有一个name变量。platform_driver从字面上来看就知道是设备驱动。设备驱动是为谁服务的呢?当然是设备了。platform_device就描述了设备对象。下面是一个具体的实例:

struct platform_device s3c_device_iis = {
    .name         = "s3c2410-iis",
    .id         = -1,
    .num_resources     = ARRAY_SIZE(s3c_iis_resource),
    .resource     = s3c_iis_resource,
    .dev = {
        .dma_mask = &s3c_device_iis_dmamask,
        .coherent_dma_mask = 0xffffffffUL
    }
};

    它的name变量和刚才上面的platform_driver的name变量是一致的,内核正是通过这个一致性来为驱动程序找到资源,即platform_device中的resource。这个结构的定义如下,位于include/linux/ioport.h中:

struct resource {
    resource_size_t start;
    resource_size_t end;
    const char *name;
    unsigned long flags;
    struct resource *parent, *sibling, *child;
};

    下面是一个具体的实例:

static struct resource s3c_iis_resource[] = {
    [0] = {
        .start = S3C24XX_PA_IIS,
        .end = S3C24XX_PA_IIS + S3C24XX_SZ_IIS -1,
        .flags = IORESOURCE_MEM,
    }
};

    这个结构的作用就是告诉驱动程序设备的起始地址和终止地址和设备的端口类型。这里的地址指的是物理地址。

    另外还需要注意platform_device中的device结构,它详细描述了设备的情况,定义如下:

struct device {
    struct klist        klist_children;
    struct klist_node    knode_parent;        /* node in sibling list */
    struct klist_node    knode_driver;
    struct klist_node    knode_bus;
    struct device        *parent;

    struct kobject kobj;
    char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
    struct device_type    *type;
    unsigned        is_registered:1;
    unsigned        uevent_suppress:1;

    struct semaphore    sem;    /* semaphore to synchronize calls to
                     * its driver.
                     */


    struct bus_type    * bus;        /* type of bus device is on */
    struct device_driver *driver;    /* which driver has allocated this
                     device */

    void        *driver_data;    /* data private to the driver */
    void        *platform_data;    /* Platform specific data, device
                     core doesn't touch it */

    struct dev_pm_info    power;

#ifdef CONFIG_NUMA
    int        numa_node;    /* NUMA node this device is close to */
#endif
    u64        *dma_mask;    /* dma mask (if dma'able device) */
    u64        coherent_dma_mask;/* Like dma_mask, but for
                     alloc_coherent mappings as
                     not all hardware supports
                     64 bit addresses for consistent
                     allocations such descriptors. */


    struct list_head    dma_pools;    /* dma pools (if dma'ble) */

    struct dma_coherent_mem    *dma_mem; /* internal for coherent mem
                     override */

    /* arch specific additions */
    struct dev_archdata    archdata;

    spinlock_t        devres_lock;
    struct list_head    devres_head;

    /* class_device migration path */
    struct list_head    node;
    struct class        *class;
    dev_t            devt;        /* dev_t, creates the sysfs "dev" */
    struct attribute_group    **groups;    /* optional groups */

    void    (*release)(struct device * dev);
};

    上面把驱动程序中涉及到的主要结构都介绍了,下面主要说一下驱动程序中怎样对这个结构进行处理,以使驱动程序能运行。

 

紧接上面介绍的数据结构,介绍驱动程序的具体实现过程。

 
    相信大家都知道module_init()这个宏。驱动模块加载的时候会调用这个宏。它接收一个函数为参数,作为它的参数的函数将会对上面提到的platform_driver进行处理。看一个实例:假如这里module_init要接收的参数为s3c2410_uda1341_init这个函数,下面是这个函数的定义:

static int __init s3c2410_uda1341_init(void) {
 memzero(&input_stream, sizeof(audio_stream_t));
 memzero(&output_stream, sizeof(audio_stream_t));
 return platform_driver_register(&s3c2410iis_driver);
}

注意函数体的最后一行,它调用的是platform_driver_register这个函数。这个函数定义于driver/base/platform.c中,原型如下:

int platform_driver_register(struct platform_driver *drv)

它的功能就是为上面提到的plarform_driver中的driver这个结构中的probe、remove这些变量指定功能函数。

    到目前为止,内核就已经知道了有这么一个驱动模块。内核启动的时候,就会调用与该驱动相关的probe函数。我们来看一下probe函数实现了什么功能。

    probe函数的原型为

    int xxx_probe(struct platform_device *pdev)

    即它的返回类型为int,接收一个platform_device类型的指针作为参数。返回类型就是我们熟悉的错误代码了,而接收的这个参数呢,我们上面已经说过,驱动程序为设备服务,就需要知道设备的信息。而这个参数,就包含了与设备相关的信息。

    probe函数接收到plarform_device这个参数后,就需要从中提取出需要的信息。它一般会通过调用内核提供的platform_get_resource和platform_get_irq等函数来获得相关信息。如通过platform_get_resource获得设备的起始地址后,可以对其进行request_mem_region和ioremap等操作,以便应用程序对其进行操作。通过platform_get_irq得到设备的中断号以后,就可以调用request_irq函数来向系统申请中断。这些操作在设备驱动程序中一般都要完成。

    在完成了上面这些工作和一些其他必须的初始化操作后,就可以向系统注册我们在/dev目录下能看在的设备文件了。举一个例子,在音频芯片的驱动中,就可以调用register_sound_dsp来注册一个dsp设备文件,lcd的驱动中就可以调用register_framebuffer来注册fb设备文件。这个工作完成以后,系统中就有我们需要的设备文件了。而和设备文件相关的操作都是通过一个file_operations 来实现的。在调用register_sound_dsp等函数的时候,就需要传递一个file_operations 类型的指针。这个指针就提供了可以供用户空间调用的write、read等函数。file_operations结构的定义位于include/linux/fs.h中,列出如下:

struct file_operations {
    struct module *owner;
    loff_t (*llseek) (struct file *, loff_t, int);
    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
    ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
    ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
    int (*readdir) (struct file *, void *, filldir_t);
    unsigned int (*poll) (struct file *, struct poll_table_struct *);
    int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
    long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
    int (*mmap) (struct file *, struct vm_area_struct *);
    int (*open) (struct inode *, struct file *);
    int (*flush) (struct file *, fl_owner_t id);
    int (*release) (struct inode *, struct file *);
    int (*fsync) (struct file *, struct dentry *, int datasync);
    int (*aio_fsync) (struct kiocb *, int datasync);
    int (*fasync) (int, struct file *, int);
    int (*lock) (struct file *, int, struct file_lock *);
    ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
    unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
    int (*check_flags)(int);
    int (*dir_notify)(struct file *filp, unsigned long arg);
    int (*flock) (struct file *, int, struct file_lock *);
    ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
    ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
    int (*setlease)(struct file *, long, struct file_lock **);
};

    到目前为止,probe函数的功能就完成了。

    当用户打开一个设备,并调用其read、write等函数的时候,就可以通过上面的file_operations来找到相关的函数。所以,用户驱动程序还需要实现这些函数,具体实现和相关的设备有密切的关系,这里就不再介绍了。

 

设备树学习(九、platform_device和platform_driver如何让匹配)   关于这个问题,我之前在分析2.6内核的时候是已经分析过的。 https://blog.youkuaiyun.com/qq_16777851/article/details/81350037   同时在总线驱动模型章节也是从基础知识学习,到最终实现过一个简单的bus的,想要深入学习,最好看下面这个专栏。(基于4.x内核) https://blog.youkuaiyun.com/qq_16777851/colu... 阅读详情

相关推荐

注册platform_device和platform_driver流程

驱动的init会在start_kernel的过程中,调用对每一个注册等级的函数依次调用加载。只针对设备树的情况:Linux内核中将设备树节点转化为平台设备platform_device的关键初始化函数是,它会自动遍历设备树,为符合条件的节点创建platform_deviceNOTE] 符合条件为1、节点必须有compatible属性没有compatible的节点(如/aliases/chosen)会被跳过2、节点不能是 reserved 或 disabled。

mangofu的博客 72

深入解析Linux Platform_device 及驱动

深入解析Linux Platform_device 及驱动 [导读] 前文分析了Linux设备驱动的驱动模型,本文来聊聊Platform_driver/Platform_device这个类。做嵌入式Linux的驱动,这个也是绕不开的,所以来学习分析总结一下。 上文阅读: 注:代码分析基于linux-5.4.31 为什么有Platform_driver 前文谈到的总线驱动模型(注这个图是照着bootlin的文档绘制的): 同时,根据代码分析其基础数据结构框架关系如下(UML关系并不严谨,仅为理

u010783226的专栏 1762

【知识图谱】一文看懂知识图谱的搭建实践与科普教程(5万字完结篇)

所有可能的实体对:(“Mate40”,“华为”), (“Mate40”,“手机”), (“华为”,“手机”)。送入关系模型分类,预期输出 (“Mate40”,“华为”): 品牌, (“Mate40”,“手机”): 属于类别, (“华为”,“手机”): 无关系。,定义了图谱中概念和关系的类型层次结构,相当于知识图谱的“纲领”和“schema”。训练完成后,当模型看到新的句子,比如“苹果的MacBook笔记本性能强大”,识别出实体“苹果”(品牌)和“MacBook”(产品)后,就能正确输出关系“品牌”。

weixin_41645791的博客 2614

关于platform_device一些讲解

从2.6版本开始引入了platform这个概念,在开发底层驱动程序时,首先要确认的就是设备的资源信息,例如设备的地址, 在2.6内核中将每个设备的资源用结构platform_device来描述,该结构体定义在kernel\include\linux\platform_device.h中: struct platform_device { const char * name; u32 id; struct device dev; u32 num_resources; struct resou

Gick 6361

【codex教程】Codex 注册和使用教程(2026最新版)

Codex 是 OpenAI 推出的 AI 编程助手,可以帮助开发者阅读代码、修改代码、运行命令、生成测试、排查 bug、做代码审查,也可以在终端、IDE、Codex App 或网页端使用。根据 OpenAI 帮助中心,截至 2026 年 6 月,Codex 已包含在 Free、Go、Plus、Pro、Business、Edu 和 Enterprise 等 ChatGPT 方案中,但不同套餐的使用额度会有所不同。适合开发者在终端中使用。如果你想在命令行里使用 Codex,可以安装 Codex CLI。

与时俱进,开放共享 2万+

Dify-前端本地运行-单点登录

本文介绍了如何在已部署Dify的系统中实现AI应用跳转及页面定制化改造。通过iframe跳转无法满足甲方需求后,作者对Dify前端源码进行了本地化二次开发,主要步骤包括:1) 按照官方文档搭建本地开发环境;2) 修改.env配置文件连接部署好的后端服务;3) 隐藏侧边栏和头部导航栏;4) 实现单点登录功能,通过URL参数获取用户邮箱自动登录。过程中解决了请求拦截导致的登录校验问题,最终达成仅保留对话框界面并同步用户信息的需求。该方案涉及Next.js框架和React技术的应用,为类似定制化需求提供了参

weixin_43163457的博客 3224

Linux内核平台设备驱动模型platform_device和platform_driver框架

关于Linux内核的平台设备驱动模型框架

D584640810的博客 4390

如何下载最新版本的VMware Workstation

随着虚拟化技术的发展,VMware Workstation作为行业领先的桌面虚拟化软件,为用户提供了在单一物理计算机上运行多个操作系统的能力。本文将详细介绍如何从Broadcom支持网站下载最新的VMware Workstation Pro版本。

安呀智数据坊的博客 669

Linux驱动_Platform平台驱动

在Linux操作系统中,驱动程序占用了Linux内核代码量的大头,如果不进行管理,将会造成数量庞大的结果。因此引入了驱动的分离。 上图所示就是驱动的分离,相当于驱动使用标准方法去获取到设备信息(比如从设备树中获取到设备信息),然后根据获取到的设备信息来初始化设备。 这样就相当于驱动只负责驱动,设备只负责设备,想办法将两者进行匹配即可。这个就是 Linux 中的总线(bus)、驱动(driver)和设备(device)模型,也就是常说的驱动分离。总线就是驱动和设备信息的月老...

qq_42174306的博客 4872

单视图几何Vanish Point(消失点/灭点)计算方法——Robert_T_Collins(罗伯特·柯林斯)算法

单视图几何Vanish Point(消失点/灭点)计算方法——Robert_T_Collins(罗伯特·柯林斯)算法

beyond951的博客 7333

全覆盖路径规划开源项目Clean-robot-turtlebot3关键函数解析(上)

本系列文章主要介绍全覆盖路径规划开源项目Clean-robot-turtlebot3的相关内容,包含如下四篇文章,分别介绍了开源项目Clean-robot-turtlebot3流程的概括总结、ROS坐标系常用坐标及其变换、Clean-robot-turtlebot3关键函数解析等内容。

慕羽★的博客 2778

platform平台设备总线一(device和driver的匹配)

一、总线的作用? 在 Linux 内核 的设备驱动模型中,关心总线、设备和驱动这 3 个实体,总线将设备和驱动绑定。 在系统每注册一个设备的时候, 会寻找与之匹配的驱动; 相反的, 在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。一个现实的Linux 设备和驱动通常都需要挂接在一种总线上,对于不属于某一实际总线的设备,比如 led, lcd,rtc 等设备, 系统为了对它们进行统一管理, 虚拟出了一条总线出来,称为 platform(平台) 总线, 相应的设备称为 platform_de

玫瑰花店的博客 1290

如何用Python编程实现算术割圆术?

第10章 割圆术 Python编程要点 from … import …语句 math模块的sqrt()函数 round()函数 10.1 图解九章 {:–}觚( gū ):正多边形。“六觚”即正六边形,“十二觚”即正十二边形。 图10-1 六觚、十二觚、二十四觚 提示 图10-1所示由图解九章源码中的“图解割圆术.py”自动绘制而成,我们可运行脚本文件,查看动态的绘制过程。 割六觚以...

人邮异步社区 3237

platform device和platform driver

从Linux 2.6起引入了一套新的驱动管理和注册机制:Platform_device和Platform_driver。 Linux中大部分的设备驱动,都可以使用这套机制, 设备用Platform_device表示,驱动用Platform_driver进行注册。 Li

zoe的专栏 1592

基于STM32设计的ECG+PPG人体参数测量系统(华为云IOT)(217)

该系统的核心是基于STM32F103RCT6微控制器,这是一种高性能、低功耗的嵌入式处理器,适用于各种复杂的控制和数据处理任务。系统利用先进的传感器技术,如光电传感器PulseSensor模块来准确测量人体脉搏血氧饱和度及心率(PPG),同时采用AD8232生物电信号采集模块捕捉并处理人体的心电图信号(ECG),从而全面反映用户的心脏活动状态。

1万+

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Platform_device和platform_driver

<br />通过Platform机制开发发底层驱动的大致流程为: 定义 platform_device---注册 platform_device ---定义 platform_driver-----注册 platform_driver。 <br />1. Platform_device 定义于 kernel/include/linux/platform_device.h中,<br />struct platform_device {<br /> const char * name;<br /> u32 id

嵌入精灵 3086

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

platform_device 和 platform_driver 驱动管理(转载)

从Linux 2.6起引入了一套新的驱动管理和注册机制:Platform_device和Platform_driver。Linux中大部分的设备驱动,都可以使用这套机制, 设备用Platform_device表示,驱动用Platform_driver进行注册。Linux platform driver机制和传统的device driver 机制(通过driver_register函数进行注册)相比,一个十分明显的优势在于platform机制将设备本身的资源注册进内核,由内核统一管理,在驱动程序中使用这些资源

woniuguilai的专栏 929

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Linux驱动开发之Platform_device和Platform_driver

从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver 。 Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 Platform_device 表示,驱动用 Platform_driver 进行注册。   Linux platform driver 机制和传统的 device d

为了忘却的记忆 959

设备树 — platform_device和platform_driver如何让匹配

可以发先追溯到底,是利用"compatible"来匹配的,即设备树加载之后,内核会自动把设备树节点转换成 platform_device这种格式,同时把名字放到of_node这个地方。id_tabel是根据id_table表中的每一个和设备名字进行匹配,这样一个驱动可以支持多个名称的设备。1.ti的omap8250驱动可以支持好多个型号的芯片,其它芯片只要这个的驱动基础上做很小的改动就可通用。当然除了第一个之外,其它的只要没匹配到,后面的几个匹配还会继续执行的。主要是在匹配函数里面的支持设备树。

开往春天的地铁 920

驱动的解读之——platform_device和platform_driver

驱动的解读之——platform_device和platform_driver  转贴自:http://blog.chinaunix.net/u1/57747/showart_1073860.html首先介绍一下注册一个驱动的步骤:1、定义一个platform_driver结构2、初始化这个结构,指定其probe、remove等函数,并初始化其中的driver变量3、实现其prob

fengya415的专栏 899

linux 内核驱动--Platform Device和Platform_driver注册过程

从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device和Platform_driver。 Linux 中大部分的设备驱动,都可以使用这套机制 ,设备用 Platform_device 表示,驱动用 Platform_driver 进行注册。 Linux platform driver 机制和传统的 device driver机制 ( 通过 driver_register 函数进行注册 ) 相比,一个十分明显的优势在于 platform 机制将设备...

baidu_38310096的博客 1184
上一篇: Linux中/proc目录下文件详解
下一篇: Linux Netlink Source Example
flymachine
博客等级 码龄23年 253粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值