知乐空间

Linux内核必备知识点(platform_driver)

平台驱动程序(Linux内核的基本知识)

平台总线是学习linux驱动时必须掌握的知识点。

本文参考已经发表:Linux 3.14内核

一.概念

嵌入式系统中有很多物理总线:I2c、SPI、USB、uart、PCIE、APB、AHB。

从2.6开始,Linux加入了新的驱动管理和注册机制。平台总线是虚拟总线,不是物理总线。

与PCI和USB相比,它主要用于描述SOC上的片上资源。平台描述的资源有一个共同点:都是在CPU的总线上直接访问的。

平台被分配一个名称(用于驱动程序绑定)和一系列资源,如地址和中断请求号(IRQ)。

设备由platform_device表示,驱动程序由platform_driver注册。

与传统的总线/设备/驱动机制相比,平台由内核统一管理,使用驱动中的资源,提高了代码的安全性和可移植性。

第二,平台

1.平台总线最重要的两种结构

平台维护的所有驱动程序都必须用以下结构定义:

平台驱动程序

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

该结构用于将驱动程序注册到平台总线,

写驱动的时候,我们经常需要填写上面的成员。

平台_设备

平台总线是一种用于描述设备硬件信息的结构,包括硬件的所有资源(io、内存、中断、DMA等)。).

struct platform_device { const char *name; int  id; bool  id_auto; 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;};

必须实现结构设备开发->发布()。

描述硬件信息的成员结构资源

0x139d0000

struct resource { resource_size_t start;  //表示资源的起始值,            resource_size_t end;    //表示资源的最后一个字节的地址, 如果是中断,end和satrt相同 const char *name;   // 可不写   unsigned long flags; //资源的类型 struct resource *parent, *sibling, *child;};flags的类型说明#define IORESOURCE_MEM  0x00000200    //内存#define IORESOURCE_IRQ  0x00000400    //中断

所有由内核管理的驱动程序必须包含一个名为struct device_driver的成员,所描述的硬件必须包含struct device结构的成员。/

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

其中包括:

const char  *name;

用于与硬件匹配。

内核描述硬件,必须包含结构设备结构的成员:

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_pm_domain *pm_domain;#ifdef CONFIG_PINCTRL struct dev_pin_info *pins;#endif#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 */#ifdef CONFIG_DMA_CMA struct cma *cma_area;  /* contiguous memory area for dma        allocations */#endif /* arch specific additions */ struct dev_archdata archdata; struct device_node *of_node; /* associated device tree node */ struct acpi_dev_node acpi_node; /* associated ACPI device node */ dev_t   devt; /* dev_t, creates the sysfs "dev" */ u32   id; /* device instance */ 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); struct iommu_group *iommu_group; bool   offline_disabled:1; bool   offline:1;};

其中包括:

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

它不能是空。

2.如何注册

向注册平台驱动的步骤

1)注册设备平台_设备_寄存器

/** * platform_device_register - add a platform-level device * @pdev: platform device we're adding */int platform_device_register(struct platform_device *pdev){ device_initialize(&pdev->dev); arch_setup_pdev_archdata(pdev); return platform_device_add(pdev);}

2)注册驱动程序平台_驱动程序_寄存器

#define platform_driver_register(drv) \ __platform_driver_register(drv, THIS_MODULE)

三.例子

1.开发步骤

平台总线下驱动程序的开发步骤如下:

装备

要实现的结构是:platform_device。

1)初始化资源结构变量

2)初始化平台设备结构变量

3)向系统注册设备:platform_device_register。

以上三个步骤必须在设备驱动加载之前,也就是platform_driver_register()执行之前完成,因为驱动注册需要匹配内核中所有注册的设备名称。

将platform_driver_register()中的设备添加到内核最终调用的device_add函数中。

Platform_device_add和device_add的主要区别是多了一步insert_resource(p,r),即平台资源被添加到内核中,由内核管理。

驱动器

在驱动注册中,要实现的结构是:platform_driver。

在驱动程序的初始化函数中,调用platform_driver_register()来注册platform_driver。

需要注意的是,platform_driver和platform_device中的name变量的值必须相同[不考虑设备树,我们稍后会写一篇关于设备树的新文章]。

这样,当注册platform_driver_register()时,当前注册的platform_driver中的name变量值将与所有注册的platform_devices中的name变量值进行比较。只有找到同名的platform _ device才能注册成功。

注册成功后,将调用platform_driver结构元素的探测函数指针。

例1

这个例子比较简单,只用来测试platform_driver和platform_device能否匹配成功。

左边是platform_device结构注册的代码,右边是platform_driver结构注册的代码。

Platform_driver定义和注册:

 1 #include   2 #include   3 #include   4 #include   5   6 static int hello_probe(struct platform_device *pdev)  7 {  8     printk("match ok \n");  9     return 0; 10 } 11 static  int hello_remove(struct platform_device *pdev) 12 { 13     printk("hello_remove \n"); 14     return 0; 15 } 16 static struct platform_driver hello_driver = 17 { 18     .probe = hello_probe, 19     .driver.name = "duang", 20     .remove = hello_remove,      21 }; 22 static int hello_init(void) 23 { 24     printk("hello_init \n"); 25     return platform_driver_register(&hello_driver); 26 } 27 static void hello_exit(void) 28 { 29     printk("hello_exit \n"); 30     platform_driver_unregister(&hello_driver); 31     return; 32 } 33 MODULE_LICENSE("GPL"); 34 module_init(hello_init); 35 module_exit(hello_exit);

Platform _设备定义和注册:

  1 #include                                                                                                                                                           2 #include   3 #include   4 #include   5   6 static void hello_release(struct device *dev)  7 {  8      return;  9 } 10 static struct platform_device hello_device = 11 { 12     .name = "duang", 13     .id = -1, 14     .dev.release = hello_release, 15 }; 16  17  18 static int hello_init(void) 19 { 20     printk("hello_init \n"); 21     return platform_device_register(&hello_device); 22  23 } 24 static void hello_exit(void) 25 { 26     printk("hello_exit \n"); 27     platform_device_unregister(&hello_device); 28     return; 29 } 30 MODULE_LICENSE("GPL"); 31 module_init(hello_init); 32 module_exit(hello_exit);

本程序仅用于测试平台框架是否能成功匹配。StructPlatform _ Device hello _ Device未设置任何硬件信息。

Makfile

  1 ifneq ($(KERNELRELEASE),)                                                                                                                                                        2 obj-m:=device.o driver.o  3 else  4 KDIR :=/lib/modules/$(shell uname -r)/build  5 PWD  :=$(shell pwd)  6 all:  7     make -C $(KDIR) M=$(PWD) modules  8 clean:  9     rm -f *.ko *.o *.mod.o *.symvers *.cmd  *.mod.c *.order 10 endif

makefile可以同时将两个C文件编译成ko文件。

编译:

编辑和翻译

生成的文件:

输入程序片

清空log信息sudo dmesg -c

匹配成功

例2

将硬件信息添加到platform_device结构中,可以在内核中读出。本示例将以下信息添加到hello_device结构中:

基址寄存器0x139d0000的地址是介于空之间的0x4。

中断号199【注意】在实际内核中,会根据HW id计算出一个新的中断号(通常当soc厂商的设备为soc时,会为每个中断源定义一个唯一的id),这个中断号会被cpu识别出来。

设备c

struct resource res[]={ [0] ={  .start = 0x139d0000,  .end  = 0x139d0000 + 0x3,  .flags = IORESOURCE_MEM, }, [1] ={  .start = 199,  .end  = 199,  .flags = IORESOURCE_IRQ, }, };static struct platform_device hello_device = { .name = "duang", .id = -1, .dev.release = hello_release,  .num_resources = ARRAY_SIZE(res), .resource = res,};

driver.c

static int hello_probe(struct platform_device *pdev){ printk("match ok \n"); printk("mem = %x \n",pdev->resource[0].start); printk("irq = %d \n",pdev->resource[1].start); //注册中断、申请内存 return 0;}

再次编译,卸载第一个示例的模块,并清除日志:

makesudo rmmod device sudo rmmod driversudo dmesg -c

执行

结果表明,探头功能正确读取了硬件信息。

4.platform_device是如何管理的?

1.没有设备树

在没有设备树的情况下,以三星Cortex-A8 s5pc100为例,将硬件信息放在以下位置

arch\arm\mach-s5pc100\Mach-smdkc100.carch\arm\plat-samsung\

注册平台_设备

平台_设备定义

这个数组存储内核启动时需要初始化的硬件信息。

2.如果有设备树

内核中会有一个完整的设备初始化代码,在内核启动时会解析并初始化设备树信息,并将硬件信息初始化到对应的链表中。总线匹配成功后,硬件信息将传递给probe()函数。

四、与公交相关的其他知识点

1.内核总线相关的结构变量

内核维护的所有总线都需要注册一个具有以下结构的变量。

struct bus_type { const char  *name; const char  *dev_name; struct device  *dev_root; struct device_attribute *dev_attrs; /* use dev_groups instead */ const struct attribute_group **bus_groups; const struct attribute_group **dev_groups; const struct attribute_group **drv_groups; 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 (*online)(struct device *dev); int (*offline)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); const struct dev_pm_ops *pm; struct iommu_ops *iommu_ops; struct subsys_private *p; struct lock_class_key lock_key;};

平台总线变量struct Bus _ Type平台总线_ 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,};

最重要的成员是**。匹配**。

当设备的硬件信息注册在platform_bus_type总线上时,它将遍历平台总线维护的所有驱动程序,并按名称进行匹配。如果硬件信息相同,说明硬件信息与驱动匹配,将调用驱动的platform_driver ->probe函数,初始化驱动的所有资源,使驱动有效。

当设备的驱动程序在platform_bus_type总线上注册时,它将遍历平台总线维护的所有硬件信息,并通过名称进行匹配。如果相同,说明硬件信息与驱动匹配,会调用驱动的platform_driver ->probe函数,初始化驱动的所有资源,使驱动有效。

注册地点

drivers\base\Platform.c

平台总线类型的注册

动词 (verb的缩写)注册代码流程的详细说明

该架构的优势在于它可以帮助我们定位问题。

1.匹配函数什么时候被调用?

2.何时调用探测函数

以下是上述两个问题代码的调用流程:

代码调用过程

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 ZLME@xxxxxxxx@hotmail.com 举报,一经查实,立刻删除。

留言与评论(共有 0 条评论)
验证码: