platform_device驱动模型用到的结构体

本文深入探讨了Linux操作系统中平台设备驱动模型的结构和原理,包括总线、设备、驱动之间的关系,以及如何实现设备的注册和驱动的注册过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://my.oschina.net/yuyang/blog/85888
linux  platform_device驱动模型详解 

platform在linux模型结构中,其实就是一种虚拟总线,没有对应的硬件结构。 
其主要用来管理系统的外设资源,比如IO 内存  中断信号...... 
这样内核就可以假设所有设备都是挂在platform总线上,从而进行整体管理。 

linux2.6的设备模型:设备、总线、类和驱动彼此相关的模型结构 

首先是总线,bus_type 
include/linux/device.h 
struct bus_type { 
    const char      *name; 
    struct bus_attribute    *bus_attrs; 
    struct device_attribute *dev_attrs; 
    struct driver_attribute *drv_attrs; 

    int (*match)(struct device *dev, struct device_driver *drv); 
    int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 
    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 dev_pm_ops *pm; 

    struct subsys_private *p; 
}; 
platform的总线定义如下: 
struct bus_type platform_bus_type = { 
    .name       = "platform", 
    .dev_attrs  = platform_dev_attrs,  //设备属性 
    .match      = platform_match,       //match函数这个函数在当属于platform的设备或者驱动注册到内核时就会调用完成设备与驱动的匹配工作 
    .uevent     = platform_uevent,      //热插拔操作函数 
    .pm     = &platform_dev_pm_ops, 
}; 
platform_bus_type不是以模块的形势注册到内核中的,它是由init/main.c中的 
do_base_setup  ---->drivers/base/init.c中driver_init----> 
drivers/base/platform.c下的 
int __init platform_bus_init(void) 

    int error; 

    early_platform_cleanup(); 

    error = device_register(&platform_bus); 
    if (error) 
        return error; 
    error =  bus_register(&platform_bus_type); 
    if (error) 
        device_unregister(&platform_bus); 
    return error; 



设备 
系统中每个设备都有一个device对象(C语言也可以进行面向对象编程,参考“Linux设备模型理解”)描述,如下 
include/linux/device.h 
struct device { 
    struct device       *parent; 

    struct device_private   *p; 

    struct kobject kobj; 
    const char      *init_name; /* initial name of the device */ 
    const struct device_type *type; 

    struct mutex        mutex;  /* mutex 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        *platform_data; /* Platform specific data, device 
                       core doesn't touch it */ 
    struct dev_pm_info  power; 
    struct dev_power_domain *pwr_domain; 

#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 device_dma_parameters *dma_parms; 

    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; 

    struct device_node  *of_node; /* associated device tree node */ 

    dev_t           devt;   /* dev_t, creates the sysfs "dev" */ 

    dev_t           devt;   /* dev_t, creates the sysfs "dev" */ 

    spinlock_t      devres_lock; 
    struct list_head    devres_head; 

    struct klist_node   knode_class; 
    struct class        *class; 
    const struct attribute_group **groups;  /* optional groups */ 

    void    (*release)(struct device *dev); 
}; 
include/linux/platform_device.h中platform_device 
struct platform_device { 
    const char  * name; 
    int     id; 
    struct device   dev; 
    u32     num_resources; 
    struct resource * resource; 

    const struct platform_device_id *id_entry; 

    /* MFD cell pointer */ 
    struct mfd_cell *mfd_cell; 

    /* arch specific additions */ 
    struct pdev_archdata    archdata; 
}; 
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; 
}; 

设备的注册: 
register_device(dev); 
int device_register(struct device *dev) 

device_initialize(dev); 
return device_add(dev); 

对应的platform_device的注册函数为:drivers/base/platform.c下platform_device_register 
int platform_device_register(struct device *dev) 

   device_initialize(&pdev->dev); 
   returnplatform_device_add(pdev); 
} 
就是mini2440的所有外设资源了,外设的具体定义在 
/arch/arm/plat-s3c24xx/devs.c下 

驱动 
下面是设备驱动定义: 
include/linux/device.h 
struct device_driver { 
const char  * name; 
struct bus_type  * bus;//所属总线 
struct completion unloaded; 
struct kobject  kobj;//代表自身 
struct klist  klist_devices;//设备列表 
struct klist_node knode_bus; 
struct module  * owner; 
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的结构如下: 
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 (*resume)(struct platform_device *); 
    struct device_driver driver; 
    const struct platform_device_id *id_table; 
}; 

驱动注册函数: 
drivers/base/driver.c下int driver_register(struct device_driver *drv) 

platform_driver的注册函数在drivers/base/platform.c下 
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; 

    return driver_register(&drv->driver); 
<think>我们正在讨论Linux设备模型中的三个关键结构体platform_deviceplatform_driver和device_driver。 它们之间的关系是: 1. platform_device 代表一个具体的平台设备,通常描述SoC上的外设(如I2C控制器、SPI控制器等),它包含设备的资源(如内存地址、中断号等)。 2. device_driver 是一个通用的设备驱动结构体,包含驱动的基本操作(如probe、remove)和设备模型需要的字段。 3. platform_driver 是平台设备驱动,它内嵌了一个device_driver结构体,并扩展了平台设备特有的操作(如平台设备匹配和电源管理)。 具体关系如下: - platform_driver 结构体中包含一个 device_driver 结构体成员(通常命名为driver)。 - platform_driver 通过其内部的 device_driver 成员与内核设备模型关联。 - 当注册一个 platform_driver 时,实际上是通过其内部的 device_driver 注册到总线上(platform_bus_type)。 - platform_deviceplatform_driver 通过名称进行匹配(即platform_device的name字段和platform_driver的id_table或者driver.name匹配)。 - 匹配成功后,会调用 platform_driver 中的 probe 函数。 结构体定义(简化): ```c struct platform_device { const char *name; // 设备名称,用于匹配驱动 int id; struct device dev; // 内嵌的通用设备结构体 // ... 资源信息等 }; struct device_driver { const char *name; // 驱动名称 struct bus_type *bus; // 所属总线 int (*probe)(struct device *dev); // 探测设备 int (*remove)(struct device *dev); // 移除设备 // ... 其他操作 }; struct platform_driver { int (*probe)(struct platform_device *); // 平台设备的探测函数 int (*remove)(struct platform_device *); // 移除函数 struct device_driver driver; // 内嵌的通用设备驱动 const struct platform_device_id *id_table; // 设备匹配表 }; ``` 工作流程: 1. 系统启动时,平台设备(platform_device)通常通过板级文件或设备树被注册到内核。 2. 驱动程序通过调用`platform_driver_register`注册一个platform_driver。 3. 内核的platform总线(platform_bus_type)会尝试将已注册的设备与驱动进行匹配。 4. 匹配依据:platform_driver的driver.name与platform_device的name匹配,或者platform_driver的id_table中的任一条目与platform_device的name匹配。 5. 匹配成功则调用platform_driver的probe函数(注意:实际调用的是platform_driver->driver.probe,但通常这个函数指针被设置为一个内部函数,该内部函数会调用platform_driver->probe,并将device转换为platform_device)。 注意:在platform_driver的初始化中,通常会将device_driver的probe函数设置为一个通用的平台驱动probe函数(例如platform_drv_probe),这个函数会调用platform_driver自己的probe函数。 总结: - platform_device 描述硬件设备。 - platform_driver 包含驱动该硬件设备的函数,并内嵌一个device_driver。 - device_driver 是设备驱动的通用部分,由platform_driver中的driver成员实现。 关系图: platform_driver -> 包含 device_driver platform_device -> 包含 device 在总线上,device_driver和device进行匹配,而实际上匹配的是platform_deviceplatform_driver。 §§相关问题§§ 1. 在内核中,platform_deviceplatform_driver是如何通过名称进行匹配的? 2. 当platform_driver注册时,其内嵌的device_driver的probe函数是如何被设置并最终调用到platform_driver的probe函数的? 3. 设备树(Device Tree)机制下,platform_device是如何被创建和注册的? 4. platform_device结构体中的struct device dev成员与device_driver结构体有何关联? 5. 在platform驱动模型中,为什么需要将device_driver内嵌在platform_driver中,而不是直接使用device_driver?</think>### Linux设备驱动模型核心结构体关系解析 #### 1. **基础层级关系** ```c struct device_driver { // 通用驱动抽象 const char *name; // 驱动名称 struct bus_type *bus; // 所属总线 int (*probe)(struct device *dev); // 探测设备 ... }; struct platform_driver { // 平台设备驱动 int (*probe)(struct platform_device *); struct device_driver driver; // 内嵌通用驱动结构 const struct platform_device_id *id_table; ... }; struct platform_device { // 平台设备实例 const char *name; // 设备名称 struct device dev; // 内嵌设备基类 struct resource *resource; // 设备资源 ... }; ``` #### 2. **关键交互机制** 1. **注册绑定流程** ```mermaid graph LR A[platform_device_register] --> B[注册到platform_bus] C[platform_driver_register] --> D[注册driver到platform_bus] B & D --> E[总线匹配driver.name与device.name] E --> F[调用platform_driver.probe] ``` 2. **结构体内嵌关系** - `platform_driver` 包含 `device_driver` 成员 - `platform_device` 包含 `device` 成员 - 通过`container_of`宏实现父子结构转换: ```c struct platform_device *pdev = container_of(dev, struct platform_device, dev); struct platform_driver *pdrv = container_of(drv, struct platform_driver, driver); ``` #### 3. **功能对比表** | 结构体 | 作用域 | 关键成员 | 生命周期管理 | |------------------|-------------|----------------------------|-------------------| | `device_driver` | 通用驱动模型 | name, bus, probe | 所有设备类型通用 | | `platform_driver`| 平台设备驱动 | driver, probe, id_table | 封装platform特有操作 | | `platform_device`| 平台设备实例 | name, dev, resource | 描述SoC固定资源 | #### 4. **典型驱动开发流程** ```c // 定义平台驱动 static struct platform_driver my_driver = { .probe = my_probe, .driver = { .name = "my_device", // 匹配设备名称 .owner = THIS_MODULE, }, }; // 设备探测函数 static int my_probe(struct platform_device *pdev) { // 1. 获取设备资源 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); // 2. 映射寄存器 void __iomem *base = devm_ioremap_resource(&pdev->dev, res); // 3. 注册字符设备等操作 ... } // 注册驱动 module_platform_driver(my_driver); ``` #### 5. **设备树(Device Tree)整合** 现代Linux使用设备树替代硬编码`platform_device`: ```dts my_device: my_device@0x12340000 { compatible = "vendor,my-device"; // 匹配驱动 reg = <0x12340000 0x1000>; // 寄存器空间 interrupts = <0 45 4>; // 中断号 }; ``` 驱动通过`of_match_table`匹配: ```c static const struct of_device_id my_of_ids[] = { { .compatible = "vendor,my-device" }, {} }; struct platform_driver my_driver = { .driver = { .of_match_table = my_of_ids, }, }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值