Linux驱动---platform总线

1、什么是总线

SOC上:数据总线、地址总线、控制总线
物理总线:USB、I2C、SPI
虚拟总线-Linux内核中:设备树依赖Platform

在这里插入图片描述

2、为什么引入platform

为了让硬件信息和驱动程序分离
举例:串口驱动、网口驱动

设备树

3、内核如果表示总线

在这里插入图片描述

每一个总线必须要定义有个叫bus_type的这样有个结构体变量,比如platform就要定义一个叫platform_bus_type这样一个结构体变量
bus_register用来注册一个总线

4、platform架构

在这里插入图片描述

platform总线要会维护两个链表,上面一个表示硬件,下面表示驱动
platform_device表示硬件信息的结构体,platform_driver表示软件信息的结构体

通过platform_bus_type里面的match函数进行匹配,在我们利用bus_register进行注册的时候就会调用match函数。
match函数会从platform_driver链表里面挨个节点挨个节点去遍历一遍,去找到struct device_driver driver或者const struct platform_device_id *id_table找到某个名字,与platform_device里面的const char *name或者struct device dev的某一个名字进行匹配。一旦匹配成功之后,说明现在有硬件有驱动了,它就会调用驱动里面的int (*probe)(struct platform_ device *);函数。
所以int (*probe)(struct platform_ device *);函数非常重要,我们基于platform总线来写驱动的时候,要把申请设备号、创建设备节点、申请内存等所有的操作都放在probe里面。

5、注册

#define platform_driver_register(drv)\
 _platform_driver_register(drv, THIS_MODOULE) //是一个宏
//驱动的注册

int platform_device_register(struct platform_device *pdev);
//硬件信息的注册

6、示例代码

device:(硬件设备)

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform.h>

void hello_release(struct device *dev)
{
	printk("hello_release");
	return;
}

struct platform_device hello_device = {
	.name = "zheng",
	.id = -1,
	.dev.release = hello_release,
};

static int hello_init(void)
{
	printk("hello_init\n");
	return platform_device_register(&hello_device);
}

static void hello_exit(void)
{
	printk("hello_exit\n");
	platform_device_unregister(&hello_device);
	return;
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

driver:(驱动)

#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform.h>

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

int hello_remove(struct platform_device *pdev)
{
	printk("hello_remove");
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.driver.name = "zheng",
};

static int hello_init(void)
{
	printk("hello_init");
	return platform_driver_register(&hello_driver);
}

static void hello_exit(void)
{
	printk("hello_exit");
	platform_driver_unregister(&hello_driver);
	return;
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

7、效果展示

加载硬件设备的时候,初始化成功
在这里插入图片描述


第一个hello_init是device打印的,第二个hello_init是driverd打印的,match ok是hello_probe打印的

我们去注册这个hello_driver的时候,把platform_driver注册到platform总线上,然后拿这个“zheng”的名字去遍历device链表(也去匹配platform_device里的名字)。匹配成功后,调用hello_driver里的probe函数。
在这里插入图片描述


1、首先调用了device里的hello_exit()
2、接着调用了hello_remove,因为我们在hello_exit()里面调用了platform_device_unregister(&hello_device);以后,就会从device链表里面把该硬件信息拿掉,这时就会调用hello_driver里面的这个remove函数
3、接着hello_device里面的hello_release()也要调用一遍
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值