13_Platform 设备驱动

Platform 设备驱动

1.什么是平台总线模型?

平台总线模型也叫platform总线模型。是Linux内核虚拟出来的一条总线,不是真实的导线。

平台总线模型就是把原来的驱动C文件给分成了俩个C文件,一个是device.c,一个是driver.c

把稳定不变的放在driver.c里面,需要变得就放在了device.c里面。

image-20240423224714016

2.为什么会有平台总线模型?

(1)可以提高代码的重用性

(2)减少重复性代码。

设备 总线 驱动

device.c driver.c

3.平台总线模型的优点。

(1) 驱动和设备代码是分开的,重用性强,可以减少重用代码

​ (2) 都挂载在platform总线上

4.怎么编写以平台总线模型设计的驱动?

一个是device.c,一个是driver.c,然后分别注册device.c和driver.c。

平台总线是以名字来匹配的,实际上就是字符串比较。

5.平台总线注册一个device

device.c里面写的是硬件资源,这里的硬件资源是指寄存器的地址,中断号,时钟等硬件资源。

在 platform 平台下用 platform_device 这个结构体表示 platform 设备, 如果内核支持设备树的话就不用使用 platform_device 来描述设备了, 因为改用设备树去描述了 platform_device。

在Linux内核里面,我们是用一个结构体来描述硬件资源的。具体定义在内核源码/include/linux/platform_device.h 里面, 结构体内容如下:

struct platform_device {
    const char *name; //platform 设备的名字,用来和 platform 驱动相匹配。 名字相同才能匹配成功。
    int id; //设备id,一般写-1
    bool id_auto;
    struct device dev; //内嵌的device结构体
    u32 num_resources; //资源的个数
    struct resource *resource; //device里面的硬件资源。
    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;
};
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 toits driver. */
	struct bus_type	*bus;		/* type of bus device is on */
	struct device_driver *driver;	/* which driver has allocated thisdevice */
	void		*platform_data;	/* Platform specific data, device core doesn't touch it */
	void		*driver_data;	/* Driver data, set and get with dev_set/get_drvdata */
	struct dev_pm_info	power;
	struct dev_pm_domain	*pm_domain;
	......
	void	(*release)(struct device *dev);
	struct iommu_group	*iommu_group;
	bool			offline_disabled:1;
	bool			offline:1;
};
struct resource {
    resource_size_t start; //资源的起始信息,对于内存类的资源,就表示内存起始地址
    resource_size_t end; //资源的终止信息, 对于内存类的资源,就表示内存终止地址
    const char *name; //资源名字
    unsigned long flags; //资源类型,可选的资源类型都定义在了文件 include/linux/ioport.h 里面
    struct resource *parent, *sibling, *child;
};

​ 参数unsigned long flags

#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */
#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */
#define IORESOURCE_IO 0x00000100 //IO的内存
#define IORESOURCE_MEM 0x00000200 //表述一段物理内存
#define IORESOURCE_REG 0x00000300 /* Register offsets */
#define IORESOURCE_IRQ 0x00000400 //表示中断
#define IORESOURCE_DMA 0x00000800
#define IORESOURCE_BUS 0x00001000
......
/* PCI control bits. Shares IORESOURCE_BITS with above PCI ROM. */
#define IORESOURCE_PCI_FIXED (1<<4) /* Do not move resource */

​ 在不支持设备树的 Linux 内核版本中需要在通过 platform_device 结构体来描述设备信息, 然后使用platform_device_register 函数将设备信息注册到 Linux 内核中, 此函数原型如下所示:

int platform_device_register(struct platform_device *pdev)

​ 如果内核支持设备树的话就不用使用 platform_device 来描述设备了, 因为改用设备树去描述了platform_device。

注销设备信息

void platform_device_unregister(struct platform_device *pdev)

示例

device.c

#include <linux/init.h>   //包含宏定义的头文件
#include <linux/module.h> //包含初始化加载模块的头文件
#include <linux/platform_device.h> //平台设备所需要的头文件

struct resource beep_res[] = {
    [0] = {
        .start = 0x020AC000, //GPIO5_01 的数据寄存器的地址 0x020AC000
        .end = 0x020AC003,
        .flags = IORESOURCE_MEM, //表述一段物理内存
        .name = "GPIO5_DR"
    }
};

void beep_release(struct device *dev)
{
    printk("beep_release\n");
}

/* platform 设备结构体 */
struct platform_device beep_device = {
    .name = "beep_test", // platform 设备的名字,用来和 platform 驱动相匹配。 名字相同才能匹配成功。
    .id = -1,           // 设备id,一个设备写-1
    .num_resources = ARRAY_SIZE(beep_res), // 资源的个数
    .resource = beep_res, // device里面的硬件资源。
    .dev = {
        .release = beep_release //设备信息卸载时调用
    }
};

static int beep_device_init(void)
{
    int ret;
    /* 内核打印函数不能用printf,因为内核没有办法使用C语言库 */
    printk("hello world\n"); // 内核模块加载的时候打印hello world
    ret = platform_device_register(&beep_device); //设备信息注册到 Linux 内核中
    return ret;
}

static void beep_device_exit(void)
{
    printk("byby\n"); // 内核模块卸载的时候打印"byb byb
    platform_device_unregister(&beep_device); //设备信息卸载
}

module_init(beep_device_init); // 驱动模块的入口
module_exit(beep_device_exit); // 驱动模块的出口

MODULE_LICENSE("GPL"); // 声明模块拥有开源许可证

Makefile

obj-m +=device.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga 
PWD?=$(shell pwd)

all:
	make -C $(KDIR) M=$(PWD) modules

运行到开发板上,查看设备 "/sys/bus/platform/devices/"

image-20240424000134861

注册platform驱动

​ 在 Linux 内核中, 用 platform_driver 结构体表示 platform 驱动, platform_driver 结构体定义指定名称的平台设备驱动注册函数和平台设备驱动注销函数, 此结构体定义在文件 include/linux/platform_device.h中, 内容如下:

struct platform_driver {
    /*当 driver 和 device 匹配成功的时候, 就会执行 probe 函数*/
    int (*probe)(struct platform_device *);
    /*当 driver 和 device 任意一个 remove 的时候, 就会执行这个函数*/
    int (*remove)(struct platform_device *);
    /*当设备收到 shutdown 命令的时候, 就会执行这个函数*/
    void (*shutdown)(struct platform_device *);
    /*当设备收到 suspend 命令的时候, 就会执行这个函数*/
    int (*suspend)(struct platform_device *, pm_message_t state);
    /*当设备收到 resume 命令的时候, 就会执行这个函数*/
    int (*resume)(struct platform_device *);
    // 内置的 device_driver 结构体
    struct device_driver driver;
    // 该设备驱动支持的设备的列表 他是通过这个指针去指向 platform_device_id 类型的数组
    const struct platform_device_id *id_table;
    bool prevent_deferred_probe;
};

​ id_table 表保存了很多 id 信息。 这些 id 信息存放着这个 platformd 驱动所支持的驱动类型。 id_table是个表(也就是数组), 每个元素的类型为 platform_device_id, platform_device_id 结构体内容如下:

struct platform_device_id {
    char name[PLATFORM_NAME_SIZE];
    kernel_ulong_t driver_data;
};

​ device_driver 结构体定义在 include/linux/device.h, device_driver 结构体内容如下:

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

​ 当of_match_table的name和name都有值时,优先匹配of_match_table的name

​ of_match_table 就是采用设备树的时候驱动使用的匹配表, 同样是数组, 每个匹配项都为of_device_id 结构体类型, 此结构体定义在文件 include/linux/mod_devicetable.h 中, 内容如下:

struct of_device_id {
    char name[32];
    char type[32];
    char compatible[128];
    const void *data;
};

​ compatible 成员, 在支持设备树的内核中, 就是通过设备节点的 compatible 属 性值和 of_match_table中每个项目的 compatible 成员变量进行比较, 如果有相等的就表示设备和此驱动匹配成功。
​ 在编写 platform 驱动的时候, 首先定义一个 platform_driver 结构体变量, 然后实现结构体中的各个成员变量, 重点是实现匹配方法以及 probe 函数。 当驱动和设备匹配成功以后 probe 函数就会执行, 驱动程序具体功能的实现在 probe 函数里面编写。
​ platform 驱动的注册使用 platform_driver_register 函数来实现, 函数原型如下:

int platform_driver_register (struct platform_driver *driver)

​ 如果注册成功,它会返回0。如果注册失败,它会返回一个负的错误码。

​ 参数 driver 为预先创建的 platform_driver 结构体。
​ 通过 platform_driver_unregister 函数来卸载 platform 驱动, 函数原型如下:

void platform_driver_unregister(struct platform_driver *drv)

示例

driver.c

#include <linux/init.h>   //包含宏定义的头文件
#include <linux/module.h> //包含初始化加载模块的头文件
#include <linux/platform_device.h> //平台设备所需要的头文件

int beep_probe(struct platform_device *platform_device)
{
    printk("beep_probe\n");
    return 0;
}

int beep_remove(struct platform_device *platform_device)
{
    printk("beep_remove\n");
    return 0;
}

struct platform_device_id beep_id_table = {
    .name = "123"
};

/* platform 驱动结构体 */
struct platform_driver beep_platform_driver = {
    .probe = beep_probe,
    .remove = beep_remove,
    .driver = {
        .owner = THIS_MODULE,
        .name = "beep_test", //匹配设备时用到的名字
    },
    // .id_table = &beep_id_table //实现了.id_table中的name,就不会匹配.driver中的name
};

static int beep_driver_init(void)
{
    int ret = 0;
    printk("hello world\n");
    ret = platform_driver_register(&beep_platform_driver); //注册平台驱动
    if(ret < 0)
    {
        printk("platform_driver_register is error\n");
        return ret;
    }
    return 0;
}

static void beep_driver_exit(void)
{
    printk("byby\n"); // 内核模块卸载的时候打印"byb byb
    platform_driver_unregister(&beep_platform_driver);
}

module_init(beep_driver_init); // 驱动模块的入口
module_exit(beep_driver_exit); // 驱动模块的出口

MODULE_LICENSE("GPL"); // 声明模块拥有开源许可证

Makefile

obj-m +=driver.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga 
PWD?=$(shell pwd)

all:
	make -C $(KDIR) M=$(PWD) modules

image-20240424120850930

平台总线probe函数编写

​ (1) 从 device.c 里面获得硬件资源, 因为我们的平台总线将驱动拆成了俩部分, 第一部分是 device.c, 另一部分是 driver.c。 那么匹配成功了之后, driver.c 要从 device.c 中获得硬件资源, 那么 driver.c 就是在 probe 函数中获得的。
​ ( 2) 获得硬件资源之后, 就可以在 probe 函数中注册杂项/字符设备, 完善 file_operation 结构体, 并生成设备节点。
获得硬件资源有两种方法:

方法一: 直接获取, 不推荐

int beep_probe(struct platform_device *pdev){
    printk("beep_probe\n");
    return 0;
}	

​ beep_probe 函数里面有一个形参 pdev, 他指向了 platform_device, 那么我们可以直接通过指针访问结构体 platform_device 的成员变量。 比如说我要访问 beep_device 中的 beep_res 中的 name, 参考如下的代码

struct resource beep_res[] = {
    [0] ={
        .start = 0x020AC000,
        .end = 0x020AC003,
        .flags = IORESOURCE_MEM,
        .name = "GPIO5_DR",
	}
};
struct platform_device beep_device = {
    .name = "beep_test",
    .id = -1,
    .resource = beep_res,
    .num_resources = ARRAY_SIZE(beep_res),
    .dev = {
    	.release = beep_release
    }
};

​ 那么 probe 函数可以直接获取, 如下所示:

int beep_probe(struct platform_device *pdev){
    printk("beep_probe\n");
    printk("beep_res is %s\n",pdev->resource[0].name); //GPIO5_DR
    return 0;
}

​ 修改完毕后, 编译 driver.c 文件, 加载驱动模块后, 如下图所示;

image-20240424121354759

方法二: 使用函数来获取资源

​ 我们也可以使用函数来获取资源, 函数如下表所示:

image-20240424121436377

​ 返回值为NULL,没有获取到资源

申请 I/O 内存

​ 向内核申请(报告)需要映射的内存资源

​ 申请 I/O 内存使用 request_region 函数, 其定义在内核源码/include/linux/ioport.h 里面, 如下图所示:

image-20240424132442144

#define request_mem_region(start,n,name) __request_region(&iomem_resource, (start), (n), (name), 0)

struct resource * __request_region(struct resource *parent,
				   resource_size_t start, resource_size_t n,
				   const char *name, int flags)

​ 第一个参数: 起始地址, 第二个参数: 长度, 第三个参数: 名字
​ Linux把基于I/O映射方式的I/O端口和基于内存映射方式的I/O端口资源统称为“I/O区域”(I/O Region)。I/O Region 仍然是一种 I/O 资源, 因此它仍然可以用 resource 结构类型来描述。
​ Linux 是以一种倒置的树形结构来管理每一类 I/O 资源(如: I/O 端口、 外设内存、 DMA 和 IRQ) 的。 每一类 I/O 资源都对应有一颗倒置的资源树, 树中的每一个节点都是一个 resource 结构, 而树的根结点 root则描述了该类资源的整个资源空间。 其实说白了, request_mem_region 函数并没有做实际性的映射工作,只是告诉内核要使用一块内存地址, 声明占有, 也方便内核管理这些资源。

释放I/O内存

释放申请的内存

#define release_mem_region(start,n)	__release_region(&iomem_resource, (start), (n))

void __release_region(struct resource *parent, resource_size_t start,
			resource_size_t n)

第一个参数: 起始地址, 第二个参数: 长度

注意: 映射建立时,是要先申请再映射;然后使用;使用完要解除映射时要先解除映射再释放申请的内存。

示例:写一个基于platform 平台驱动模型的蜂鸣器设备的驱动。

driver.c

#include <linux/init.h>   //包含宏定义的头文件
#include <linux/module.h> //包含初始化加载模块的头文件
#include <linux/platform_device.h> //平台设备所需要的头文件
#include <linux/ioport.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>

struct resource *beep_mem;
struct resource *beep_mem_tmp;

unsigned int *vir_gpio5_dr; //蜂鸣器虚拟地址

int misc_open(struct inode *inode, struct file *file)
{
    printk("hello misc_open\n");
    return 0;
}

int misc_release(struct inode *inode, struct file *file)
{
    printk("hello mise_release bye bye\n");
    return 0;
}

ssize_t misc_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff_t)
{
    char kbuf[64] = "heheh";
    if (copy_to_user(ubuf, kbuf, strlen(kbuf) + 1) != 0)
    {
        printk("copy_to_user error\n");
        return -1;
    }
    printk("hello misc_read bye bye\n");
    return 0;
}

ssize_t misc_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff_t)
{
    char kbuf[64] = {0};
    if (copy_from_user(kbuf, ubuf, size) != 0)
    {
        printk("copy_from_user error\n");
        return -1;
    }
    printk("hello misc_write bye bye\n");
    if(kbuf[0] == 1) //打开蜂鸣器
    {
        *vir_gpio5_dr |= 0x02; 
    }else if(kbuf[0] == 0)//关闭蜂鸣器
    {
        *vir_gpio5_dr &= ~0x02; 
    }
    return 0;
}

/* 文件操作集 */
struct file_operations misc_fops = {
    .owner = THIS_MODULE, // 当前模块
    .open = misc_open,
    .release = misc_release,
    .write = misc_write,
    .read = misc_read,
};

/* 杂项设备结构体 */
struct miscdevice misc_dev = {
    .minor = MISC_DYNAMIC_MINOR, // 动态分配次设备号
    .name = "hello_mise",        // 设备节点的名字
    .fops = &misc_fops           // 文件操作集

};

int beep_probe(struct platform_device *pdev)
{
    int ret = 0;
    printk("beep_probe\n");
    // printk("pdev->resource[0].name is %s\n", pdev->resource[0].name); //方法一,直接获取
    beep_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); //方法二,使用函数来获取资源
    if(beep_mem == NULL)
    {
        printk("platform_get_resource is error\n");
        return -EBUSY;
    }
    printk("beep_res start is %x\n", beep_mem->start);
    printk("beep_res end is %x\n", beep_mem->end);
    // beep_mem_tmp = request_mem_region(beep_mem->start, beep_mem->end-beep_mem->start+1, "beep"); //	向内核申请(报告)需要映射的内存资源
    // if(beep_mem_tmp == NULL)
    // {
    //     printk("request_mem_region is error\n");
    //     goto err_region;
    // }
    ret = misc_register(&misc_dev); // 注册杂项设备
    if (ret < 0)
    {
        printk("misc register is error!\n");
        return -1;
    }
    printk("mise register is ok!\n");
    vir_gpio5_dr = ioremap(beep_mem->start, beep_mem->end-beep_mem->start+1); //将物理地址转化为虚拟地址
    if(vir_gpio5_dr == NULL)
    {
        printk("GPIO5_DR ioremap is error!\n");
        return -EBUSY;
    }
    printk("GPIO5_DR ioremap is ok!\n");
    return 0;
    
    // err_region:
    //     release_mem_region(beep_mem->start, beep_mem->end-beep_mem->start+1); //释放申请的内存
    //     return -EBUSY;
}

int beep_remove(struct platform_device *platform_device)
{
    printk("beep_remove\n");
    return 0;
}

struct platform_device_id beep_id_table = {
    .name = "123"
};

/* platform 驱动结构体 */
struct platform_driver beep_platform_driver = {
    .probe = beep_probe,
    .remove = beep_remove,
    .driver = {
        .owner = THIS_MODULE,
        .name = "beep_test", //匹配设备时用到的名字
    },
    // .id_table = &beep_id_table //实现了.id_table中的name,就不会匹配.driver中的name
};

static int beep_driver_init(void)
{
    int ret = 0;
    printk("hello world\n");
    ret = platform_driver_register(&beep_platform_driver); //注册平台驱动
    if(ret < 0)
    {
        printk("platform_driver_register is error\n");
        return ret;
    }
    return 0;
}

static void beep_driver_exit(void)
{
    printk("byby\n"); // 内核模块卸载的时候打印"byb byb
    platform_driver_unregister(&beep_platform_driver);
    misc_deregister(&misc_dev); // 注销杂项设备
    iounmap(vir_gpio5_dr); //卸载杂项设备
}

module_init(beep_driver_init); // 驱动模块的入口
module_exit(beep_driver_exit); // 驱动模块的出口

MODULE_LICENSE("GPL"); // 声明模块拥有开源许可证

device.c

#include <linux/init.h>   //包含宏定义的头文件
#include <linux/module.h> //包含初始化加载模块的头文件
#include <linux/platform_device.h> //平台设备所需要的头文件

struct resource beep_res[] = {
    [0] = {
        .start = 0x020AC000, //GPIO5_01 的数据寄存器的地址 0x020AC000
        .end = 0x020AC003,
        .flags = IORESOURCE_MEM, //表述一段物理内存
        .name = "GPIO5_DR"
    }
};

void beep_release(struct device *dev)
{
    printk("beep_release\n");
}

/* platform 设备结构体 */
struct platform_device beep_device = {
    .name = "beep_test", // platform 设备的名字,用来和 platform 驱动相匹配。 名字相同才能匹配成功。
    .id = -1,           // 设备id,一个设备写-1
    .num_resources = ARRAY_SIZE(beep_res), // 资源的个数
    .resource = beep_res, // device里面的硬件资源。
    .dev = {
        .release = beep_release //设备信息卸载时调用
    }
};

static int beep_device_init(void)
{
    int ret;
    /* 内核打印函数不能用printf,因为内核没有办法使用C语言库 */
    printk("hello world\n"); // 内核模块加载的时候打印hello world
    ret = platform_device_register(&beep_device); //设备信息注册到 Linux 内核中
    return ret;
}

static void beep_device_exit(void)
{
    printk("byby\n"); // 内核模块卸载的时候打印"byb byb
    platform_device_unregister(&beep_device); //设备信息卸载
}

module_init(beep_device_init); // 驱动模块的入口
module_exit(beep_device_exit); // 驱动模块的出口

MODULE_LICENSE("GPL"); // 声明模块拥有开源许可证

app.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
    int fd;
    char buf[64] = {0};
    
    if(argc < 2)
    {
        printf("Usage: %s <1:beep open / 0:beep close>\n", argv[0]);
        return -1;
    }
    
    fd = open("/dev/hello_mise", O_RDWR);
    if (fd < 0)
    {
        perror("open error\n");
        return fd;
    }
    buf[0] = atoi(argv[1]); //字符串转int
    write(fd, buf, strlen(buf)+1);
    close(fd);
    return 0;
}

Makefile

obj-m +=device.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga 
PWD?=$(shell pwd)

all:
	make -C $(KDIR) M=$(PWD) modules

Makefile

obj-m +=driver.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga 
PWD?=$(shell pwd)

all:
	make -C $(KDIR) M=$(PWD) modules
posted @ 2024-04-25 22:34  爱吃冰激凌的黄某某  阅读(44)  评论(0编辑  收藏  举报